If I have the following code:
function testA {
setTimeout('testB()', 1000);
doLong();
}
function testB {
doSomething();
}
function doLong() {
//takes a few seconds to do something
}
When I run testA()
, what happens after 1000 milliseconds, when the timeout for testB()
is reached? Is JavaScript being single-threaded relevant here?
Here are some possibilities:
testB()
will be queued up to execute afterdoLong()
and any other functions it calls are finished.doLong()
will be immediately terminated andtestB()
will start executing.doLong()
will be given some extra time to finish before being stopped (either automatically or after user prompting) and thentestB()
will begin.doLong()
will be paused, whiletestB()
starts. AftertestB()
finishes,doLong()
will resume.
What is the accurate answer to this scenario? Is it implementation-specific or part of the standard?
This related question may provide some insight, but it's not an exact match.
Feel free to share any links that could help in better understanding JavaScript execution flow.
Thank you!
*I am aware that not all browsers comply with standards :(