停止Javascript倒计时

时间:2018-01-22 18:00:25

标签: javascript

我正在研究番茄钟时间,但是当用户点击不止一次运行计数器时,我会让两个计数器工作,我想暂停计数器而不是启动另一个计数器。

function countdown(minutes) {
  let seconds = 60;
  let mins = minutes;
  function tick() {
    let current_minutes = mins-1;
    seconds--;
    time.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
    if( seconds > 0 ) {
      setTimeout(tick, 1000);
      } else {
      if(mins > 1){
          countdown(mins-1);         
      }
     }

    console.log(mins);

    if (seconds === 0 && mins === 1) {
      time.innerHTML = `${sessionLength}:00`;
      rounds++;
      console.log(rounds);
      PlaySound(sound);
    }
  }
tick();
}

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

为什么不使用setInterval?

var myPomodoro = setInterval(function(){
   //... run code in here every 60 seconds
}, 60000);

// stop the timer
clearInterval(myPomodoro);