What happens if a warning window is displayed during an ajax call?

I'm just wondering what might happen if, during an ajax call, a request appears in the browser window to the user.

Say for example that I have an ajax call

$.ajax({ url: ..., type: GET/POST, ... success: function(data){ console.log(data); }, error: ... }); 

which takes a lot of time (10 seconds). While the call is in progress, a simple javascript warning is issued.

 alert("hello!"); 

What happens, for example, if:

  • ajax call starts
  • Getting ajax call data
  • warning is displayed
  • ajax call returns data (warning window still open!)

Knowing that JS is a single thread, I know that the script will be stopped, I am just wondering what will happen to the ajax call / response if the warning window is not closed "in time".

Hope I was clear enough and that this is not a "dummy" question. Thanks you

+52
javascript ajax single-threaded
May 22 '14 at 8:19
source share
2 answers

It's not really hard to check and see ... but the answer is that the warning will take precedence and will support the flow of execution.

When the warning is closed and provided that the AJAX request is completed while the warning is open, the success (or error ) function will be processed.

Please note that the AJAX request will continue to be executed while the warning is displayed (therefore, long-running AJAX can process while the warning is open), but it is just that your script cannot continue processing the request until the warning is closed.

Here is a working example , note that data is not written to the console until the warning is closed.

Another thing to consider is that after the warning is closed, the script will continue to work with the rest of the function (the code immediately after the alert ) before the AJAX response is processed. It helps demonstrate what I mean.

+59
May 22 '14 at 8:25
source share

The HTTP request will continue to be executed and processed in the background.

When the JS event loop becomes free, the readystatechange handler fires.

Some intermediate readiness states may be skipped because the event loop was busy while that state was true.

+11
May 22 '14 at 8:26
source share



All Articles