javascript - setInterval() 在clearInterval() 之后不起作用

标签 javascript electron

我目前正在用 Javascript 制作一个带有开始和停止按钮的番茄计时器。在使用clearInterval()停止倒计时后,我遇到了使用setInterval()开始倒计时的问题。我有两个函数,单击按钮即可调用,一个函数使用 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;
}

关于javascript - setInterval() 在clearInterval() 之后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60863275/

相关文章:

vue.js - Vuejs 字符串格式无法正常工作

angularjs - Angular ,如何组合模块

node.js - electron-rebuild 如何控制重建的 node_module_version ?

javascript - 如何在手机中更改背景图像?

javascript - 用于测试的 Cypress 劈弦

javascript - Firefox 扩展 - PageMod 脚本修改窗口

ElectronJs : app. quit 导致应用程序崩溃

javascript - 重新定义点击选择器 jQuery

javascript - 查找破坏 IE 的 ECMAScript/JavaScript 错误

javascript - 在 Electron 中嵌入外部链接