setinterval倒计时工作不正常

setinterval countdown is not working properly

本文关键字:不正常 工作 倒计时 setinterval      更新时间:2023-09-26

我有js倒计时功能

var sTime = new Date().getTime();
var countDown = 60;
function RefreshThePage() {
var cTime = new Date().getTime();
var diff = cTime - sTime;
var seconds = countDown - Math.floor(diff / 1000);
if (seconds === 0) {
    location.reload(true);
}
if (seconds >= 0) {
    var minutes = Math.floor(seconds / 60);
    seconds -= minutes * 60;
    $("#minutes").text(minutes < 10 ? "0" + minutes : minutes);
    $("#seconds").text(seconds < 10 ? "0" + seconds : seconds);
} else {
    $("#countdown").hide();
    clearInterval(counter);
}
}

在html方面,我称之为

 RefreshThePage();
           var counter = setInterval(RefreshThePage, 1000);

问题是,通常它应该在60秒内刷新页面,但此代码在30秒内刷新页面。我不知道问题出在哪里。。

我解决了问题

我在ui端的某个地方忘记了这个html元标签

<meta http-equiv="refresh" content="30" />