我有这样的事情:
var activityTextToggleTimerTwo = setInterval(function() {
active_users_text.toggleClass("active inactive bounce bounceOutUp")
var activityTextToggleTimerThree = setTimeout(function() {
active_users_text.toggleClass("active inactive bounce bounceOutUp");
}, 5000);
}, 25000);
我尝试清除超时/间隔,如下所示:
clearInterval( activityTextToggleTimerTwo );
clearTimeout( activityTextToggleTimerThree );
我得到一个例外:
Uncaught ReferenceError: activityTextToggleTimerThree is not defined
为什么呢?另外我认为activityTextToggleTimerThree没有被清除..
由于
答案 0 :(得分:1)
您的变量超出范围,因为它在setInterval
的回调中定义。你必须将它移动到外部范围,但是你可能仍然有问题:每次执行setInterval
回调时,你都将替换该变量中的计时器处理程序,所以你要做到这一点只能清除最新的setTimeout
计时器。
答案 1 :(得分:0)
将.clearInterval()
方法放在.setInterval()
匿名函数中,例如:
// assuming you have active_users_text defined
var rotations = 0;
var activityTextToggleTimerTwo = setInterval(function(){
active_users_text.toggleClass('active inactive bounce bounceOutUp');
// change 1 if you want
if(rotations < 1){
var activityTextToggleTimerThree = setTimeout(function(){
active_users_text.toggleClass('active inactive bounce bounceOutUp');
}, 5000);
}
// change 75000 if you want
if(rotations++ == 75000){
clearInterval(activityTextToggleTimerTwo);
}
}, 25000);
setTimeout()
不需要使用此方法清除。