js实现活动倒计时
let startTime = 1527647143949; // 开始时间
var time = new Countdown('timer',startTime);
function Countdown (el,startTime) {
this.startTime = startTime || '';
this.el = el || '';
// 轮询计算时间
this.loop = function () {
var that = this;
setInterval(function(){
that.init();
},1000);
};
// 格式化时分秒
this.formatDuring = function (mss) {
var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
var seconds = parseInt((mss % (1000 * 60)) / 1000);
return hours + ": " + (minutes < 10 ? '0'+minutes : minutes) + ": " + (seconds < 10 ? '0'+seconds : seconds);
};
// 初始化倒计时
this.init = function () {
var endTime = this.startTime+(24*60*60*1000); // 结束时间
var timeLeft = endTime - new Date().getTime(); // 剩余时间
document.getElementById(this.el).innerHTML = this.formatDuring(timeLeft);
this.loop();
};
this.init();
};