简体   繁体   中英

How to skip 1st delay of setInterval function?

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 kindly 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM