消除setInterval倒计时误差
可以使用setTimeout递归自循环调用解决
countDown();
function addZero(i) {
return i < 10 ? "0" + i : i + "";
}
function countDown() {
var nowtime = new Date()
var endtime = new Date("2021/02/12,00:00:00");
var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);
var d = parseInt(lefttime / (24 * 60 * 60));
var h = parseInt(lefttime / (60 * 60) % 24);
var m = parseInt(lefttime / 60 % 60);
var s = parseInt(lefttime % 60);
d = addZero(d);
h = addZero(h);
m = addZero(m);
s = addZero(s);
document.querySelector(".count").innerText = `新年倒计时 ${d} 天 ${h} 时 ${m} 分 ${s} 秒 `;
if (lefttime <= 0) {
document.querySelector('.count').innerText = "新年快乐";
}
setTimeout(countDown, 1000);
}
- 点赞
- 收藏
- 关注作者
评论(0)