If I have a function with a setTimeout of 20 seconds and then statements afterwards. It will continue to process the statements after the setTimeout and then go to the statements in the setTimout call. What is the flow of the code to be expected? Once the 20 seconds gets hit do the statements after the setTimeout continue and then the ones in setTimeout, do the statements stop being processed after the setTimeout then the ones in the setTimeout and back to where it left off, etc..,? Is there a way to say process everything inline and not continue onward? It's fairly easy to get around it but knowing what exactly is going on and how to prevent it would be very helpful and much appreciated. Thanks.
The setTimeout has no relationship to anything. It goes off on its own and fires off the code when it gets executed. It has no clue that x,y,and z was siting after the call.
It is a asynch process, not a synch one. If you are looking for a pause/sleep. You need to break your function up into two parts and have the setTimeout call the second piece.
Think of the setTimout as setting a timer in a different thread. The current thread keeps on executing, and your callback function gets invoked in the timeout thread when the timer expires.
Thanks for the expected informative posts guys. Just one more, do function calls react the same? I "thought" quite awhile back I had 2 function calls after one another too that acted like separate threads. I might have had a setTimeout somewhere in place though but are simple function calls sequentially processed or are they shot of as though threads? Thanks again guys.