clearInterval() 不起作用,我不明白为什么
clearInterval() doesn't work and I can't figure out why
我正在尝试将 play/pause
按钮添加到一个计时器,该按钮具有根据 isRunning 的值决定要做什么的功能。第二次单击我的按钮而不是暂停时,计时器似乎又添加了一个 setTimeout
,我不明白为什么。
var isRunning = false;
function start() {
if(isRunning == false) {
isRunning = true;
setInterval(time, 1000);
document.getElementById("play").className = "fa fa-pause";
}
else if(isRunning == true) {
isRunning = false;
document.getElementById("play").className = "fa fa-play";
console.log(isRunning);
clearInterval(time);
}
}
document.getElementById("play").onclick = start;
var isRunning = false;
var timeoutId; // Where the timeout id will be stored.
function start() {
if(isRunning == false) {
isRunning = true;
timeoutId = setInterval(time, 1000);
document.getElementById("play").className = "fa fa-pause";
}
else if(isRunning == true) {
isRunning = false;
document.getElementById("play").className = "fa fa-play";
console.log(isRunning);
clearInterval(timeoutId); // clearInterval using timeout id saved before.
}
}
document.getElementById("play").onclick = start;
有关详细信息,请访问 https://developer.mozilla.org/es/docs/Web/API/WindowTimers/setTimeout
我正在尝试将 play/pause
按钮添加到一个计时器,该按钮具有根据 isRunning 的值决定要做什么的功能。第二次单击我的按钮而不是暂停时,计时器似乎又添加了一个 setTimeout
,我不明白为什么。
var isRunning = false;
function start() {
if(isRunning == false) {
isRunning = true;
setInterval(time, 1000);
document.getElementById("play").className = "fa fa-pause";
}
else if(isRunning == true) {
isRunning = false;
document.getElementById("play").className = "fa fa-play";
console.log(isRunning);
clearInterval(time);
}
}
document.getElementById("play").onclick = start;
var isRunning = false;
var timeoutId; // Where the timeout id will be stored.
function start() {
if(isRunning == false) {
isRunning = true;
timeoutId = setInterval(time, 1000);
document.getElementById("play").className = "fa fa-pause";
}
else if(isRunning == true) {
isRunning = false;
document.getElementById("play").className = "fa fa-play";
console.log(isRunning);
clearInterval(timeoutId); // clearInterval using timeout id saved before.
}
}
document.getElementById("play").onclick = start;
有关详细信息,请访问 https://developer.mozilla.org/es/docs/Web/API/WindowTimers/setTimeout