html倒计时某天代码,JS 倒计时(一天的倒计时)

JS 倒计时

function NextTime(next, cb) {

var t;

(function ft(){

var dif = (next.getTime() - (new Date()).getTime()) / 1000;

if(dif > 0){

t = setTimeout(ft, 1000);

if(cb)

cb(Math.floor(dif % 86400 / 3600), Math.floor(dif % 3600 / 60), Math.floor(dif % 60));

} else {

clearTimeout(t);

}

})();

return function(){

clearTimeout(t);

};

}

function lpad(num, n) {

var len = num.toString().length;

while(len < n) {

num = "0" + num;

len++;

}

return num;

}

var now = new Date();

var next = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);

new NextTime(next, function(hour, minute, second){

document.getElementById("test").innerHTML=lpad(hour, 2) + ':' + lpad(minute, 2) + ':' + lpad(second, 2);

});

你可能感兴趣的:(html倒计时某天代码)