class inspectionTime {
lastTime = "";
timer = "";
constructor (callback = function () {}, time = 60) {
this.callback = callback;
this.limtTime = time * 1000;
}
start () {
if (this.timer) {
return {
code: "0",
msg: `剩余时间${(new Date().getTime - this.lastTime) / 1000}`
};
} else {
this.lastTime = new Date().getTime();
this.run();
}
}
run () {
this.timer = setInterval(() => {
const surplusTime = parseInt(
(new Date().getTime() - this.lastTime) / 1000
);
if (surplusTime >= parseInt(this.limtTime / 1000)) {
this.timer = clearInterval(this.timer);
this.callback({
code: "1",
time: parseInt(this.limtTime / 1000) - surplusTime,
msg: `倒计时结束`
});
} else {
this.callback({
code: "0",
time: parseInt(this.limtTime / 1000) - surplusTime,
msg: `倒计时中`
});
}
}, 1000);
}
}
如何制作js的倒计时
最新推荐文章于 2023-03-11 14:25:55 发布