简体   繁体   English

setInterval() 在 clearInterval() 后不起作用

[英]setInterval() not working after clearInterval()

I am currently making a pomodoro timer in Javascript with start and stop buttons.我目前正在用 Javascript 制作一个带有开始和停止按钮的番茄钟定时器。 I'm running into issues with using setInterval() to start the countdown after it has been stopped using clearInterval().在使用 clearInterval() 停止倒计时后,我遇到了使用 setInterval() 开始倒计时的问题。 I have 2 functions which are called on click of a button, one starts the countdown using setInterval(), the other stops it using clearInterval().我有 2 个通过单击按钮调用的函数,一个使用 setInterval() 开始倒计时,另一个使用 clearInterval() 停止倒计时。 I'm not sure what's going on, this is my implementation below:我不确定发生了什么,这是我在下面的实现:

var startTime = 25; // this value will change depending on what the user selects, default value is 25:00
var time = startTime * 60; //number of seconds total
var intervalID; //used for setInterval()

var pomodoroTimer = document.getElementById('pomodoro-timer');


function updateTimer(){
    let minutes = Math.floor(time/60);
    let seconds = time % 60;

    minutes = minutes < 10 ? '0' + minutes : minutes;
    seconds = seconds < 10 ? '0' + seconds : seconds; 

    pomodoroTimer.innerHTML = minutes + ':'+ seconds;
    time--;
}

function startTimer(){
    if(!intervalID){ // to prevent multiple setIntervals being queued up
        updateTimer(); // call this once to stop clock from taking too long on first use of the setInterval function
        intervalID = setInterval(updateTimer, 1000);
    }
}

function stopTimer(){
    clearInterval(intervalID);
}
function stopTimer(){
    clearInterval(intervalID);
    intervalID = undefined;
}

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

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