0
Difference between SetInterval() and SetTimeout ()?
hi guys help me please!
5 Antworten
+ 4
+ setTimeout() run the callback function passed as argument only once (after specified amount of time in milliseconds)
+ setInterval() run the callback function infinitly, waiting for the amount of time specified between execution ended and next call...
+ setInterval() could be emulated with setTimeout() if called inside callback function:
function callback() {
/* do something */
}
setInterval(callback,200);
... is equivalent to:
function callback() {
/* do something */
setTimeout(callback,200);
}
setTimeout(callback,200);
+ 5
SetInterval() is if you want the program to wait x amount of time before it does Y. SetTimeout() waits x amount of time and does Y. SetInteral() is for loops.
+ 2
Note that whilst setInterval by itself is infinite, you can stop it with clearInterval based on your chosen criteria.
+ 2
@Duncan: that's not a deep difference between setInterval and setTimeout, as setTimeout have its own similar clearTimeout(), even it's less often required ;)
- 1
Thanks