When launched in a browser, will setTimeout ever run its code before executing the main program? Do major browser vendors agree with this behavior or is this a side effect of the implementation? (or they agreed to keep this side effect as standard behavior)
Consider a very simple (and useless) program.
setTimeout(function(){ console.log("Timeout Called") },1); for(var i=0;i<10000000;i++){}; console.log("done");
First, we set a single microsecond setTimeout callback function, which displays the Timeout Called to the console.
Then we rotate in the loop for more than a microsecond.
Then we print done to the console.
When I run this program, it always gives
done Timeout Called
That is, the setTimeout callback functions are not taken into account until the main program is launched.
Is this reliable, specific behavior? Or there are times when the main program execution will be stopped, the callback will start, and then the main program will continue.
source share