$(document).ready(function () {
var endtime = new Date("2013/3/29 15:15:00");
var nowtime = new Date();
var maxtime = (endtime.getTime() - nowtime.getTime()) / 1000;//秒
var timer = setInterval(daojishi, 1000);
daojishi();//防止页面刷新导致的时间延迟一秒才显示
if (maxtime <= 0) {
clearInterval(timer);
}
//倒计时
var lefttime = 0;//减去天数(以秒计算)后剩余的时间 减去小时(以秒计算)对应的时间。。。
function daojishi() {
if (maxtime >= 0) {
var days = Math.floor(maxtime / 3600 / 24);
lefttime = maxtime - days * 86400;
var hours = Math.floor(lefttime / 3600);
lefttime = maxtime - hours * 3600;
var minutes = Math.floor((lefttime / 60) % 60);
lefttime = maxtime - minutes * 60;
var seconds = Math.floor(lefttime % 60);
if (hours < 10)
hours = "0" + hours;
if (minutes < 10)
minutes = "0" + minutes;
if (seconds < 10)
seconds = "0" + seconds;
$("#pday").html(days);
$("#phour").html(hours);
$("#pmin").html(minutes);
$("#psec").html(seconds);
--maxtime;
}
else {
clearInterval(timer);
}
}
});
js 倒计时
最新推荐文章于 2021-04-22 18:15:07 发布