Search code examples
javascriptcountdowntimersession-storage

How can I create a session storage for my countdown timer so it's not reset after reloading the page?


This is my JavaScript file. I use the Countdown.js API , timer is a variable I receieve as a parameter.

var countDownCompleted = false;
var myCountdownTest = new Countdown({
    time: timer,
    width: 200,
    height: 80,
    onComplete: countdownComplete,
    rangeHi: "minute"
});
function countdownComplete() {
    countDownCompleted = true;
    window.location.href = 'index.php';
}

What modification can I do so the countdown does not reset after reloading the page? I don't really understand how to create session storage.


Solution

  • localStorage and sessionStorage work just like normal variables. If you want your timer to pick up where it left off when the page is reloaded, you should store the current value every time the timer goes down.

    Then, on every page load, you can check whether there's anything set inside localStorage and, if it is, start back from that value.