Countdown Timer that doesn't reset when you refresh the page?

14,475

If you know that the user will have local cache enabled and able to store data, you could save 2 values to localStorage, 1 for the currentTime and 1 for the targetTime. Then you compare the 2 in an interval and if currentTime > targetTime, display your message.

Also bind to the onbeforeunload event and save the new currentTime back into localStorage. This will give you the persisted countdown you are seeking.

Here is an example of how you can do this:

var interval;
let minutes = 1;
let currentTime = localStorage.getItem('currentTime');
let targetTime = localStorage.getItem('targetTime');
if (targetTime == null && currentTime == null) {
  currentTime = new Date();
  targetTime = new Date(currentTime.getTime() + (minutes * 60000));
  localStorage.setItem('currentTime', currentTime);
  localStorage.setItem('targetTime', targetTime);
}
else{
  currentTime = new Date(currentTime);
  targetTime = new Date(targetTime);
}

if(!checkComplete()){
  interval = setInterval(checkComplete, 1000);
}

function checkComplete() {
  if (currentTime > targetTime) {
    clearInterval(interval);
    alert("Time is up");
  } else {
    currentTime = new Date();
    document.write(
     "\n <font color=\"white\"> Seconds Remaining:" + ((targetTime - currentTime) / 1000) + "</font>");
  }
}

document.onbeforeunload = function(){
  localStorage.setItem('currentTime', currentTime);
}

Note that I would've made a snippet however stackoverflow and other online IDE's are complaining about security breaches. Note that this is perfectly valid and does not breach any security. Save it as a .js and run.

To start the "countdown" again, invoke localStorage.clear().

Share:
14,475

Related videos on Youtube

FunnyFalcon
Author by

FunnyFalcon

Updated on September 15, 2022

Comments

  • FunnyFalcon
    FunnyFalcon over 1 year

    I Need to have a code for the timer that doesn't reset the countdown timer even you refresh the page.

    I saw sample code at keith wood plug in, but don't know how to implement this in my html file. here's the link :

    http://keith-wood.name/countdown.html

    I hope somebody could help me! thanks! :)

    I just need Hour, Minutes and Seconds to show. And after the Time given, there is a prompt box that will show "Time is up! " . This one is for the online exam we are developing. Thank you so Much! :)

    • Abhilash
      Abhilash over 11 years
      There are implementation instructions at that link. Have you followed them? What errors have you encountered?
    • Bali Balo
      Bali Balo over 11 years
      You may need to use cookies to store the end date of the question (the first time you visited the page + a certain amount of time). Keep in mind that javascript is not secure at all and anyone could "cheat" at your exam. You can for exemple add an extra verification in php in order to prevent that.
  • FunnyFalcon
    FunnyFalcon over 11 years
    the one is resetting also when you refresh the page . I'm looking on how to retain the time in your timer. I have exam and there is a next button, when the user click the next, it will direct the user to next page but the time should be the continuation of the previous page. I hope you can help me on this one. Thanks! :)
  • Jeremy John
    Jeremy John over 11 years
    Why don't you put the timer in the page main body with the window.onbeforeunload = function() { return "Are you sure you want to quit this exam?"; } script and use iframes tag for each of those exam pages that way the main page won't reload.
  • Jeremy John
    Jeremy John over 11 years
    This is an Example: fiddle.jshell.net/vGKJN/1/show try closing the page and try clicking the iframe links in the page and watch the timer.