I need to execute multiple functions one after another in fixed time intervals, thus using setTimeout. I want to ensure that I understand how it executes. I have following logic:
setTimeout(function() {
//Execute first function
}, 200);
setTimeout(function() {
//Execute second function
}, 400);
setTimeout(function() {
//Execute third function
}, 600);
Does this mean that first function executes after 200ms, second one 200ms after the first, and the third one 200ms after the second and so on? Or do I need to change something.
Following James' answer, in order to guarantee this minimum delay, you should trigger the next
setTimeout()
inside the anonymous callback, each of them with the 200ms delay specified:This way you can be sure that the delay between each function will be at least 200ms
Essentially, yes that's what it means. Bear in mind however that the specification only guarantees that the delay parameter is a minimum time that must be awaited, and browsers can and do throttle these calls sometimes - notably if the tab is not active:
and:
Also, if any of your functions take a noticeable amount of time to run, the delay between the end of one function finishing and the next starting may not be 200ms - it could be less.