猿问

刷新持久性倒计时

我有一个要解决的问题,而且我不是javascript的高手,基本上是在加载我的元素ID的时候,该元素ID将在我的代码中开始从2小时分秒开始倒数,但是我有一些链接,单击这些链接可以刷新页面并正在刷新我的计数器,我发现此处的完整计数器是链接: JavaScript倒计时,添加小时和分钟


var count = 7200;

var counter = setInterval(timer, 1000); //1000 will  run it every 1 second


function timer() {

    count = count - 1;

    if (count == -1) {

        clearInterval(counter);

        return;

    }


    var seconds = count % 60;

    var minutes = Math.floor(count / 60);

    var hours = Math.floor(minutes / 60);

    minutes %= 60;

    hours %= 60;


    document.getElementById("timer").innerHTML = hours + " ora " + minutes + " minute" + seconds + " secunde ramase ";

}

我只想使此计数器在刷新时不重置。


万千封印
浏览 132回答 1
1回答

九州编程

用途localStorage:var count = parseInt(localStorage.getItem("count")) || 7200;function timer() {    //...    document.getElementById("timer").innerHTML = hours + " ora " + minutes + " minute" + seconds + " secunde ramase ";    localStorage.setItem("count", count);}要重定向页面并清除localStorage计时器的触发时间0,请将以下代码添加到函数中:function timer() {    count = count - 1;    if (count == 0) {        localStorage.removeItem("count");        window.location.href = "home.html";    }    //...}
随时随地看视频慕课网APP
我要回答