Why Does Settimeout Not Trigger After Specified Time
I am running a very basic javascript code in Viusal Studio Code. However I am not seeing the callback function get triggered. I am trying to understand how setTimeout() works in
Solution 1:
When you call setTimeout
or when you do any blocking operation for that matter, it's in fact added to an event table and not executed immediately.
It's only after the call stack is empty that the event loop check the event table to see if there's anything waiting, and move it to the call stack.
To better understand this, check the following example:
setTimeout(() =>console.log('first'), 0)
console.log('second')
You may think that first
will be logged before second
, but in fact it's second
that will be logged first.
In your example the call stack is never empty, so setTimeout
won't really work as expected.
You may wanna check this, it will give you a better idea about what's happening under the hood.
Solution 2:
While(1) is an infinite while loop. I think that is the reason
Post a Comment for "Why Does Settimeout Not Trigger After Specified Time"