我有 1 个可以打开和关闭的按钮: 按钮打开 -> onrepeat = true 按钮关闭 -> onrepeat = false
如果 onrepeat 为 true,则 setinterval 会开始执行,效果很好。如果 onrepeat 为 false,我希望它清除间隔。
我的清除间隔不会停止间隔,它只会继续:
function checkRepeat() {
var int = setInterval(repeat, 6000);
if (onrepeat === false) {
$("#repeat_toggle").attr("class", "repeat_button");
repeat();
int;
onrepeat = true;
}
else {
clearInterval(int);
$("#repeat_toggle").attr("class", "repeat_off_button");
onrepeat = false;
}
}
知道出了什么问题吗?
最佳答案
int
的作用域为函数。每次调用 checkRepeat
您需要将其范围限制在函数之上。
关于javascript - clearInterval 不会停止 setInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13729395/