countDown() {
//设置秒杀结束时间
var endTime = new Date("2021-09-17 18:51:59"),
endSeconds = endTime.getTime();
//定义变量保存剩余的时间
var d = "00",
h = "00",
m = "00",
s = "00";
//使用箭头函数this
var id = setInterval(() => {
seckill();
}, 1000);
var seckill = () => {
const _this = this;
var nowTime = new Date(); //获取当前时间
//获取时间差,单位为秒
var remaining = parseInt((endSeconds - nowTime.getTime()) / 1000);
if (remaining > 0) {
//判断秒杀是否过期
//计算剩余天数(除以60*60*24取整,获取剩余的天数)
d = parseInt(remaining / 86400);
//计算剩余小时(除以60*60转换为小时,与24小时取模,获取剩余的小时)
h = parseInt((remaining / 3600) % 24);
//计算剩余分钟(除以60转为分钟,与60取模,获取剩余的分钟)
m = parseInt((remaining / 60) % 60);
//计算剩余秒(与60取模,获取剩余的秒数)
s = parseInt(remaining % 60);
_this.day = d;
// console.log(_this.day,"this")
//统一利用两位数表示剩余的天、小时、分钟、秒
d = d < 10 ? "0" + d : d;
h = h < 10 ? "0" + h : h;
m = m < 10 ? "0" + m : m;
s = s < 10 ? "0" + s : s;
_this.days = d;
_this.hour = h;
_this.minute = m;
_this.second = s;
} else {
clearInterval(id); //秒杀过期,取消定时器
d = h = m = s = "00";
_this.days = d;
_this.hour = h;
_this.minute = m;
_this.second = s;
}
// document.getElementById(‘day‘).innerHTML = d ;
// document.getElementById(‘hour‘).innerHTML = h;
// document.getElementById(‘minute‘).innerHTML = m;
// document.getElementById(‘second‘).innerHTML = s;
};
},