簡體   English   中英

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

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

我目前正在用 Javascript 制作一個帶有開始和停止按鈕的番茄鍾定時器。 在使用 clearInterval() 停止倒計時后,我遇到了使用 setInterval() 開始倒計時的問題。 我有 2 個通過單擊按鈕調用的函數,一個使用 setInterval() 開始倒計時,另一個使用 clearInterval() 停止倒計時。 我不確定發生了什么,這是我在下面的實現:

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