首先计算倒计时的总秒数,然后定义一个定时器let timer = null
var intDiff = parseInt(60);//倒计时总秒数量
timer = window.setInterval(function() {
var day = 0,
hour = 0,
minute = 0,
second = 0; //时间默认值
if (intDiff > 0) {
day = Math.floor(intDiff / (60 * 60 * 24));
hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
}
if (hour <= 9)
hour = '0' + hour; //小于9的时候给小时添加一个0例如00:13:14
if (minute <= 9)
minute = '0' + minute;
if (second <= 9)
second = '0' + second;
if (Number(hour) == 0 && Number(minute) == 0 & Number(second) == 0){
clearInterval(timer); //时分秒均为0的时候清除定时器
$("#timer span:eq(0)").text(hour); //获取页面的时标签并更改为现在的
$("#timer span:eq(1)").text(minute);
$("#timer span:eq(2)").text(second);
intDiff--; // 秒数-1
}, 1000);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)