开发者

How to skip 1st delay of setInterval function?

开发者 https://www.devze.com 2023-03-27 15:31 出处:网络
when we use setInterval it delays for specified time and then run the function specified in 1st argument. What should i do if I dont want the delay for the 1st time?

when we use setInterval it delays for specified time and then run the function specified in 1st argument. What should i do if I dont want the delay for the 1st time?

I have one solution that call the function initially and then use setInterval so for the 1st call delay will not be there. but It is not proper solution. So If anybody have some another Solution Then ki开发者_如何转开发ndly let me know that.


setInterval(function(){
    doFunction();
}, 15000);

doFunction();

does that imediatelly :)


read this post. Changing the interval of SetInterval while it's running

Peter Bailey has a good generic function, however gnarf also has a nice little example.

You will have to create your own timer for this. SetInterval does not allow you to change the interval delay while its being executed. Using setTimeout within a callback loop should allow you to edit the interval time.


Another way:

(function run(){
        // code here
    setTimeout(run,1000);
}())


Create a wrapper for that.

function runPeriodically(func, miliseconds) {
    func();
    return setInterval(func, miliseconds);
}

function onePiece() { /* */ }
runPeriodically(onePiece, 100);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号