What happens if an alert window is shown during an ajax call?
Solution 1:
This isn't exactly hard to try and see... but the answer is that the alert will take priority and hold up the flow of execution.
Upon closing the alert, and assuming the AJAX request has completed while the alert was open, then the success
function will be processed (or error
function).
Note that the AJAX request will continue to run while the alert is shown (so a long running AJAX can process while the alert is open), but it is just that your script cannot continue handling the request until the alert is closed.
Here is a working example, notice that the data isn't written to the console until the alert is closed.
Another point to be aware of is that after the alert closes, the script will continue with the rest of the function (the code immediate after the alert
) before the AJAX response is handled. This helps demonstrate what I mean
Solution 2:
The HTTP request will continue to run and be processed in the background.
When the JS event loop becomes free, the readystatechange handler will fire.
Some intermediate ready states may be skipped because the event loop was busy while that state was true.