This can be accomplished locally (that means that each user gets their own counter) by saving the value of Date.now()
into localStorage
. Then, when reopening the html file, you read from localStorage
and compare the value to the current Date.now()
.
What you can do with this timer is then very limited, unless you serve this html file of yours from a server.
(function() {
var started = localStorage['started'];
if (started) {
// This is not the first time the user opens this file
// How long has it been?
var diff = Date.now() - started;
if (diff >= 1000 * 60 * 60 * 24 * 7) {
// At least one week has passed. Do something here.
} else {
// Less than a week has passed. Do something else here.
}
} else {
// This is the first time the user opens this file
localStorage['started'] = Date.now();
// Do something else here to welcome the user...
}
})();
Date.now reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
Just remember that there is no security at all in this. You are doing this on the user's browser, and the user has full control over that environment. A simple clear cache will delete the saved value from localStorage
.
I have used this method to create a project time clock that runs completely locally.