- Save the starting time in local storage
- Upon each page load, compare the stored time with the current time and resume the countdown from where it left off.
Here's an example implementation (not tested):
var waitTime = 900000; // 15 minutes
var executionTime;
var initialTime = localStorage.getItem("initialTime");
if (initialTime === null) {
localStorage.setItem("initialTime", (new Date()).getTime());
executionTime = waitTime;
}
else {
executionTime = parseInt(initialTime, 10) + waitTime - (new Date()).getTime();
if (executionTime < 0) executionTime = 0;
}
setTimeout(function() {
alert("Warning");
// reset the timeout to start from waitTime on page reload
localStorage.removeItem("initialTime");
}, executionTime);
Edit:
If you prefer not to use localStorage, another option is to store it in an asp.net session variable like this:
Session["InitialTime"] = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
Then apply similar logic to the JavaScript code above and convert the result to milliseconds for the setTimeout function second parameter.