我有一个要解决的问题,而且我不是javascript的高手,基本上是在加载我的元素ID的时候,该元素ID将在我的代码中开始从2小时分秒开始倒数,但是我有单击刷新的链接页面并刷新我的计数器,我在这里找到完整的计数器是链接: JavaScript count down, add hours & minutes
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 ";
}
我只想使此计数器在刷新时不重置。
答案 0 :(得分: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";
}
//...
}