0

Difference between SetInterval() and SetTimeout ()?

hi guys help me please!

10th Jan 2018, 7:40 PM
Arti Zechetto
Arti Zechetto - avatar
5 odpowiedzi
+ 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);
10th Jan 2018, 9:18 PM
visph
visph - avatar
+ 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.
10th Jan 2018, 7:53 PM
Ole113
Ole113 - avatar
+ 2
Note that whilst setInterval by itself is infinite, you can stop it with clearInterval based on your chosen criteria.
10th Jan 2018, 9:32 PM
Duncan
Duncan - avatar
+ 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 ;)
10th Jan 2018, 9:35 PM
visph
visph - avatar
- 1
Thanks
10th Jan 2018, 9:37 PM
Arti Zechetto
Arti Zechetto - avatar