Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if an alert window is shown during an ajax call?

I was just wondering what could happen if, while an ajax call is being executed, an alert is prompted to the user, in the browser window.

Let's say, for example, that I have an ajax call

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

that takes long time to complete (10 sec). While the call is executed, a simple javascript alert is thrown

alert("hello!");

What happens for example if:

  1. ajax call starts
  2. ajax call fetching data
  3. alert is shown
  4. ajax call returns data (alert window is still open!)

Knowing that JS is single threaded I know that the script execution will halt, I was just wondering what happens to the ajax call/response if the alert window is not closed "in time".

I hope I was clear enough and that this is not a "dummy" question. Thank you

like image 803
BeNdErR Avatar asked May 22 '14 08:05

BeNdErR


People also ask

What happens when one ajax call is still running and you send an another ajax call before the data of first ajax call comes back?

Since Ajax calls are asynchronous, the application will not 'pause' until an ajax call is complete, and simply start the next call immediately. JQuery offers a handler that is called when the call is successful, and another one if an error occurs during the call.

How can I send message alert in ajax?

click(function () { $. ajax({ type: "POST", url: "http://ip.jsontest.com/", crossDomain: true, data: { username: "abc", password: "1234" }, dataType: "JSONP", success: function (resdata) { console. log("success", resdata); }, error: function (result, status, err) { console.

What is error ajax call?

Many pages send AJAX requests to a server. Because this relies on the cooperation of the server and the network between the client and the server, you can expect these AJAX errors: Your JavaScript program receives an error response instead of data; Your program has to wait too long for the response.

How can I alert ajax response data?

ajax({ url:"myscript. php", dataType: "json", success:function(data){ alert(data. cenas); } });


2 Answers

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

like image 62
musefan Avatar answered Oct 18 '22 19:10

musefan


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.

like image 11
Quentin Avatar answered Oct 18 '22 17:10

Quentin