简体   繁体   中英

continue count down after refresh

The counter works properly but starts again after refresh How can I continue after refreshing the counter?

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);
  
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
  
        display.textContent = minutes + ":" + seconds;
  
        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
  }
  
  window.onload = function () {
    var duration = 60 * 2,
        display = document.getElementById("timer");
    startTimer(duration, display);
  };

you can try and use localStorage. localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session end. for example:

localStorage.setItem("duration",60);
localStorage.getItem("duration"); //60

check out this documentation: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM