4

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.

rlemon
  • 17,518
  • 14
  • 92
  • 123
Dena
  • 195
  • 1
  • 3
  • 12
  • What language are you talking about? JavaScript? Please update your question with this information (and add an appropriate tag as well) – Veger Aug 11 '11 at 12:28

4 Answers4

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

doFunction();

does that imediatelly :)

genesis
  • 50,477
  • 20
  • 96
  • 125
1

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.

Community
  • 1
  • 1
rlemon
  • 17,518
  • 14
  • 92
  • 123
1

Another way:

(function run(){
        // code here
    setTimeout(run,1000);
}())
stewe
  • 41,820
  • 13
  • 79
  • 75
0

Create a wrapper for that.

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

function onePiece() { /* */ }
runPeriodically(onePiece, 100);
viam0Zah
  • 25,949
  • 8
  • 77
  • 100