倒计时方法
调用
timeArr: ["00", "00", "00"],
this.backTimerContont({
startTime: new Date('2022-10-24 10:00:00').getTime(), //结束时间
endTime: 0,
}) // 计算倒计时
backTimerContont (initTime) {
// let timestamp = new Date(initTime.startTime).getTime() + ''
let self = this;
let timer = setInterval(function () {
let nowTime = new Date();
let endTime = new Date(initTime.startTime);
let t = endTime.getTime() - nowTime.getTime(); // 时间差
if (t > 0) {
// let day = Math.floor(t / 86400000);
let hour = Math.floor((t / 3600000) % 24);
let min = Math.floor((t / 60000) % 60);
let sec = Math.floor((t / 1000) % 60);
let hourStr = "",
minStr = "",
secStr = "";
hourStr = hour < 0 ? "--" : hour < 10 ? `0${hour}` : hour;
minStr = min < 0 ? "--" : min < 10 ? `0${min}` : min;
secStr = sec < 0 ? "--" : sec < 10 ? `0${sec}` : sec;
// let format = "";
// if (day > 0) {
// format = `${day}天${hour}小时${min}分${sec}秒`;
// }
// if (day <= 0 && hour > 0) {
// format = `${hour}小时${min}分${sec}秒`;
// }
// if (day <= 0 && hour <= 0) {
// format = `${min}分${sec}秒`;
// }
// self.content = format;
self.timeArr = [hourStr, minStr, secStr];
self.beginContest = false
} else {
clearInterval(timer);
self.beginContest = true
}
}, 1000);
},