正如标题所述,该计时器功能在控制台上运行良好,并在我调用stopChrono()
方法后5秒钟后停止,但是在webpack开发服务器上,计时器继续运行。发生了什么事?
class Timers {
constructor() {
this.running = false;
this.countForward;
}
chronometer(seconds) {
this.seconds = seconds;
this.date = Date.now();
this.now = this.date - this.seconds * 1000;
if (!this.running) {
this.running = true;
this.countForward = setInterval(() => {
this.secondsForward = Math.round((Date.now() - this.now) / 1000);
console.log(this.secondsForward);
}, 1000);
console.dir( this.countForward)
}
}
stopChrono() {
if (this.running) {
clearInterval(this.countForward);
this.running = false;
console.log('stop')
}
}
}
const chrono = new Timers();
chrono.chronometer(0);
setTimeout(() => {
chrono.stopChrono()
}, 5000);