Trying to figure out why the output of the following code is the way it is
console.log("Start");
setInterval(function() {
console.log("setInterval");
}, 5000);
let date = new Date();
while(new Date() - date <= 10000) {}
console.log("End");
Actual Output
- Start
- …. 10 seconds ….
- End
- setInterval
- …. 5 seconds ….
- setInterval
Since the while block blocks the main thread for 10 seconds, the callback function of setInterval should have been pushed twice in the task queue since it has a timer of 5 seconds. So the output should have been
Expected Output
- Start
- …. 10 seconds ….
- End
- setInterval
- setInterval
- …. 5 seconds ….
- setInterval
2
Answers
The while is blocking the thread, even if the setInterval is asynchronous and written before.
That is because javascript is running on a single-thread, so if you start a long code that isn’t asynchronous it will block the thread during the time that runs.
This does not means that anything that runs through
setInterval()
runs in a parallel process… It’s a common mistake that I myself made plenty of times.The following line literally clogs your script execution in a way that your
setInterval()
was left without room to calculate its own timelapse.After your 10 seconds ends, then your
setInterval()
is free to calculate its own timelapse again (its clock was not ticking because of the clogging). But as it fires only after 5 seconds, then it still fires after theconsole.log('End')
.Try this instead:
Or, if you really want to start your interval after 10 seconds you could do: