var countdown = null; // 定时器(定义到 加载页面时不会刷新countdown 这个值的地方)
// 投票倒计时
var endDate = new Date('${endDate }'); // 倒计时开始时间
endDate.setMinutes(endDate.getMinutes() + parseInt('${djsc }')); // 倒计时长(分钟)
var nowDate = new Date('${nowDate }'); // 当前时间
var endTime = endDate.getTime();
var nowTime = nowDate.getTime();
if(countdown != null) {
clearInterval(countdown); // 关闭定时器
}
countdown(); // 重新开启
function countdown() {
nowTime += 1000; //
var time = endTime - nowTime; // 距离结束时间的毫秒数
if(time <= 0) {
$("#countdown").html("<font color='red'>投票结束</font>");
return;
}
var h = Math.floor(time / (1000 * 60 * 60)); // 小时
var m = Math.floor(time / (1000 * 60) % 60); // 分钟
var s = Math.floor(time / 1000 % 60); // 秒
var html = (h > 9 ? h : "0" + h) + ":" + (m > 9 ? m : "0" + m) + ":" + (s > 9 ? s : "0" + s);
$("#countdown").html("投票倒计时:" + html);
countdown = setTimeout(countdown, 1000); // 定时器(每秒执行一次)
}
![](https://i-blog.csdnimg.cn/blog_migrate/3a704359294e797246a0aea2b20a00d4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7b5444991e376657cd03b1d50dfd0157.png)