I am trying to understand this piece of code but I get different results in different runtime environment: The condition:
- Print "Hello World" Every second
- And stop after 5 times
- After 5 times. Print "Done" and let Node exit.
let timerId = setInterval(() => console.log("Hello world"), 1000);
// after 5 seconds stop
setTimeout(() => {
clearInterval(timerId);
console.log("Done");
}, 5000);
In Node.js, this is printed in terminal:
The message is printed 4 times, not 5 times!
In browser's console, it is fine:
But when I add debugger to the code and run in browser, I get a completely different result:
Any clarification is appreciated, thank you