filmov
tv
Program 16 Self Invoking function | Event looping | JavaScript Interview Questions | setTimeout

Показать описание
The function is immediately invoked as soon as the script is loaded.
The first setTimeout call is made with a delay of 2000 milliseconds (2 seconds). This means that the function passed as the first argument (which logs "1") will be executed after a 2-second delay.
The second setTimeout call is made with a delay of 0 milliseconds. This may seem odd, but it actually means that the function passed as the first argument will be added to the event queue immediately, but it won't be executed until all the currently executing code has finished. This is often used to defer execution of a function until the next event loop iteration.
After all the synchronous code has finished executing, the event loop will start processing the queued events. The second setTimeout function (which logs "3") will be executed at this point.