The code they are using is likely pulling from a database with an actual value increasing live.
Check out this js fiddle I made for an example of how a simple timer can work. Notice that if you press “Run” multiple times, the time itself will stay constant.
Using a remote database will cause a lot more work, but for saving values across browser refreshes you should learn about localStorage I’d check out W3 School’s article on the subject.
In my implementation I use
localStorage.setItem("time", currentTime); // Note that CurrentTime must be a string!
in each iteration of your code after setting the currentTime
var.
When you start up your application, a simple if
statement
if (localStorage.getItem("time") {
CurrentTime = localStorage.getItem("time");
} else {
// set default values
}
will work, as localStorage.getItem
will return null
if the value doesn’t exist (or if you set the value to null
manually).
(for localStorage, you can also use [bracket notation]
and will probably see that in most common examples)
localStorage["foo"] = "value";
// is the same as:
localStorage.setItem("foo", "value")
// and
var bar = localStorage["foo"];
// is the same as:
var bar = localStorage.getItem("foo");
solved How to make a javascript count up, such as world population? [closed]