【问题标题】:Javascript: Force new loop iteration in setIntervalJavascript:在 setInterval 中强制执行新的循环迭代
【发布时间】:2012-12-28 13:06:28
【问题描述】:

我有一个 setInterval 循环。它设置为 3500 毫秒,如下所示:-

var loop = setInterval(function() { /*stuff*/ }, 3500);

如果发生某种情况,我想强制循环进行新的迭代,而不是等待 3500 毫秒。这怎么可能?是继续还是我只需要以不同的方式构建流程?

【问题讨论】:

  • 改用自调用setTimeout。更容易,更清洁,无论如何我更喜欢它。
  • 最简洁的方法是实现一个在“某些情况下”调用自身的方法。

标签: javascript loops setinterval


【解决方案1】:

您可以尝试使用setTimeout 而不是setInterval 编写匿名自调用函数:

var i = 0;

(function() {
    // stuff
    i++;
    if (i % 2 == 0) {
        // If some condition occurs inside the function, then call itself once again
        // immediately
        arguments.callee();
    } else {
        // otherwise call itself in 3 and a half seconds
        window.setTimeout(arguments.callee, 3500);
    }
})();​ // <-- call-itself immediately to start the iteration

更新:

由于在 cmets 部分中表达了对 arguments.callee 使用的分歧,以下是使用命名函数可以实现相同的方法:

var i = 0;
var doStuff = function() {
    // stuff
    i++;
    if (i % 2 == 0) {
        // If some condition occurs inside the function, then call itself once again
        // immediately
        doStuff();
    } else {
        // otherwise call itself in 3 and a half seconds
        window.setTimeout(doStuff, 3500);
    }
};
doStuff();

【讨论】:

  • 我不同意使用arguments.callee。您应该使用命名函数。我认为严格模​​式甚至不允许使用这个属性。
  • 好的,我已经更新了我的帖子,其中包含一个使用命名函数的示例。
  • 哦,很好。我的attempt is more or less the same(尽管我意识到我没有在第一个之后保存对setTimeout 的任何引用......嗯)。愚蠢的window.setTimeout 是怎么回事?
【解决方案2】:

您可以使用类似的东西...使用setTimeout 而不是setInterval...

<script type="text/javascript">
    var show;
    var done = false;

    show = setTimeout(showHideForm, 3500);

    function showHideForm() {
        // Do something

        if(done) {
            clearTimeout(show);

            show = setTimeout(showHideForm, 2000);
        }
    }
</script>

clearTimeoutsetTimeout 返回的句柄作为参数。

【讨论】:

  • 你可以(function showHideForm(){})() 这样你就没有重复的代码sn-ps。
  • 你有两次show = setTimeout(showHideForm, #000);。干,对吧?
  • @JaredFarrish 我该怎么做?
  • ()() 放在showHideForm(){} 周围(第一个括号)。有些人称其为立即调用函数或自调用函数。这没什么大不了的,但是当我在自己的代码中看到那个微重复时,它总是像小事情一样让我烦恼,你知道吗?无论如何,您可以在我的回答和达林的回答中看到它们的实际效果。
【解决方案3】:

使用命名函数并在需要时调用它。

var loop = setInterval(loopFunc, 3500);

function loopFunc(){
  //do something
}

function anticipate(){
  clearInterval(loop);  //Stop interval
  loopFunc();  //Call your function
  loop = setInterval(loopFunc, 3500);  //Reset the interval if you want
}

【讨论】:

    【解决方案4】:

    我做的例子:

    var time = 3500,
        loops = 0,
        loop;
    
    (function run(){
        var wait = time,
            dontwait = false;
    
        if (loops++ == 5) {
            loops = 0;
            dontwait = 1000;
        }
    
        console.log('Interval: ', dontwait || wait);
    
        return loop = setTimeout(run, dontwait || wait);
    })();​
    

    http://jsfiddle.net/NX43d/1/

    基本上,自调用函数在自调用函数上循环,并带有 (!) 速记变量切换。漂亮。

    【讨论】:

      【解决方案5】:
      function looper(t) {
          var loop = setInterval(function() {
              document.write(s++);
              if (mycondition) { // here is your condition
                  loopagain(200); // specify the time which new loop will do
                  loop = window.clearInterval(loop); // clear the first interval
                  return; // exit from this function!
              }
          }, t);
      }
      
      window.onload = looper(1000); // this will have default setInterval function time ans will start at window load!
      
      function loopagain(t) {
          looper(t);
      }​
      

      http://jsfiddle.net/tFCZP/

      【讨论】:

        最近更新 更多