简体   繁体   中英

How to set timer not to reset when page is reload (JavaScript)

Here is my JavaScript code. I want to avoid resetting timer when the page is refreshed.

const startingMinutes = 1;
let time = startingMinutes * 60;

const countdownEl = document.getElementById('countdown');

setInterval(updateCountDown, 1000);

    function updateCountDown() {
        const minutes = Math.floor(time/60);
        let seconds = time % 60;
    
        countdownEl.innerHTML = `${minutes}: ${seconds}`;
    
        if (time>0) {
            time--;
        }
    }

Store the current counter variable in local storage.

Hope this helps.

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