简体   繁体   English

使用 clearInterval 清除计时器不起作用

[英]Clear timer using clearInterval is not working

I put my work online here :我把我的工作放在网上:

how to reproduce :如何重现:

  • Click on a green pin on the map to choose a station单击地图上的绿色图钉以选择车站
  • Fill the two inputs name / lastname填写两个输入名称/姓氏
  • Sign the canvas在画布上签名

  • You should see a timer starting 20:00 min您应该会看到一个从 20:00 开始的计时器

  • If you cancel (annule button) the timer如果您取消(annule 按钮)计时器
  • Then resign the canvas, the timer is still going, its not reset然后退出画布,计时器仍在运行,未重置

My timer is in the file /js/timer.js I don't understand why, because I clearInterval my this.setTimer when I call the button line 59, I used timer.cancel method to clearInterval and clear the sessionStorage.我的计时器在文件 /js/timer.js 我不明白为什么,因为当我调用按钮第 59 行时,我清除了this.setTimer ,我使用timer.cancel方法来 clearInterval 并清除 sessionStorage。 But I tried to debug and weird things, this.setTimer appears to be equal to 10, then each time I cancel, this.setTimer increases by 1.但是我尝试调试和奇怪的事情, this.setTimer似乎等于10,然后我每次取消时, this.setTimer增加1。

Did I miss something ?我错过了什么 ? The proper behavior should be when I click on "annule" (cancel) the timer reset to 20:00 min正确的行为应该是当我点击“annule”(取消)计时器重置为 20:00 分钟

Here my cancel function这是我的取消功能

cancel = () => {
    this.isPlaying = false;
    clearInterval(this.setTimer);
    sessionStorage.clear();
    console.log(this.setTimer);
    canvas.clear();
    this.timer.innerHTML = 'Votre réservation a été annulée.'
    document.getElementById('reservation-content').classList.remove('hidden');
    document.getElementById('cancel-btn').className = 'hidden';
    document.getElementById('validation-btn').setAttribute('disabled', null)
}
this.delta

stores old value when you subtracted 1000.减去 1000 时存储旧值。

after clear interval you need to reset this.delta value again.在清除间隔后,您需要再次重置this.delta值。 Try adding尝试添加

this.delta = (this.endDate - this.now);

after

clearInterval(this.setTimer);

in cancel method.在取消方法中。

Hope I answered your question.希望我回答了你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM