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