0

我有这样的事情:

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没有被清除..

谢谢

4

2 回答 2

1

您的变量超出范围,因为它是在回调中定义的setInterval。您必须将其移至外部范围,但您可能仍然遇到问题:每次setInterval执行回调时,您将替换该变量中的计时器处理程序,因此您只能清除最新的setTimeout计时器。

于 2013-09-20T22:14:18.737 回答
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()不需要使用此方法清除。

于 2013-09-20T22:14:22.857 回答