我正在尝试建立一个递增计时器,即使您更改或刷新页面,它也必须计算出解决票证的时间,当该页面结束时,用户必须单击停止按钮,但是我有一个问题,当它计数时,我按下按钮Stop and reset
可以正常工作,但是当我刷新网站时,它仍在计数并且没有停止。
这是我的网站:
var countDownDate = localStorage.getItem('startDate');
if (countDownDate) {
countDownDate = new Date(countDownDate);
} else {
countDownDate = new Date();
localStorage.setItem('startDate', countDownDate);
}
var x = setInterval(function() {
var now = new Date().getTime();
var distance = now - countDownDate.getTime();
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
}, 1000);
function Reset() {
var countDownDate = localStorage.getItem('startDate');
if (countDownDate == 0, clearInterval(x)) {
countDownDate = new Date(countDownDate);
} else {
countDownDate = new Date();
localStorage.setItem('startDate', countDownDate);
}
}
<button onclick="Reset()">Stop and Reset</button>
<p id="demo"></p>