Javascript 如何立即启动 setInterval 循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10563344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:53:35  来源:igfitidea点击:

How to start setInterval loop immediately?

javascriptjquerysetinterval

提问by Googlebot

In a simple setInterval

在一个简单的 setInterval

setInterval(function() {
      // Do something every 9 seconds
}, 9000);

The first action will happen after 9 seconds (t=9s). How to force the loop to perform the first action immediately (t=0)?

第一个动作将在 9 秒 ( t=9s)后发生。如何强制循环立即执行第一个动作 ( t=0)?

I think it is due to the mechanism of setIntervalto have Delay - Action - Delay - Action ...loop; instead of Action - Delay - Action - Delay ...loop.

我认为这是由于setInterval具有Delay - Action - Delay - Action ...循环的机制;而不是Action - Delay - Action - Delay ...循环。

EDIT:My function is indeed a loop as

编辑:我的函数确实是一个循环

setInterval(function(){
$('.test').each(function(idx){
    var duration = 1000;
    $(this).delay(duration*idx);
    Some stuff here
});
}, 4000);

回答by Salman A

Keep it simple. You can use a named function instead of an anonymous function; call it and set an interval for it.

把事情简单化。您可以使用命名函数而不是匿名函数;调用它并为其设置一个间隔。

function doSomething() {
    console.log("tick");
}
doSomething();
setInterval(doSomething, 9000);

Create a scope if necessary:

如有必要,创建一个范围:

(function() {
    function doSomething() {
        console.log("tick");
    }
    doSomething();
    setInterval(doSomething, 9000);
})();

Finally, the following works without creating or affecting x:

最后,以下工作不会创建或影响x

setInterval(function x() {
    console.log("tick");
    return x;
}(), 9000);

回答by alex

Sometimes I use this pattern...

有时我使用这种模式...

(function me() {
    // Do something every 9 seconds

    setTimeout(me, 9000);
})();

It's not quite the same, as it will wait until the do somethingis executed before waiting ~9 seconds to call it again. But, this is often useful so events on the event queue do not stack up needlessly (however unlikely it is some code will take 9 seconds to run :)

它并不完全相同,因为它会等待直到执行某项操作,然后等待约 9 秒才能再次调用它。但是,这通常很有用,因此事件队列上的事件不会不必要地堆积起来(但不太可能某些代码需要 9 秒才能运行 :)

Note that in older IEs, the mewill leak to the outside scope.

请注意,在较旧的 IE 中,me将泄漏到外部范围。

回答by Richard

setInterval() is a really ugly function. I use this sanitized version, which does call the function immediately, and takes a time in seconds, BEFORE the function parameter so calling it with an inline function definition actually looks sensible.

setInterval() 是一个非常丑陋的函数。我使用这个经过消毒的版本,它会立即调用函数,并在函数参数之前花费几秒钟的时间,因此使用内联函数定义调用它实际上看起来很合理。

function startInterval(seconds, callback) {
  callback();
  return setInterval(callback, seconds * 1000);
}

回答by epascarello

Use a named function and call it and assign it to the interval.

使用命名函数并调用它并将其分配给间隔。

var myFnc = function() {
    // Do something every 9 seconds
};
setInterval(myFnc, 9000);
myFnc();

The other option is to use setTimeout instead.

另一种选择是使用 setTimeout 代替。

var myFnc = function() {
    // Do something every 9 seconds
    setTimeout(myFnc, 9000);
};
myFnc();