Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Wait until async ajax calls are finished

Hi I have 2 ajax calls in my script, I need them run asnyc to spare time, but I need the second to wait until the first is finished.

$.ajax({
    type: "POST",
    url: "getText.asmx/ws_getText",
    data: parO1,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert(msg.d.data);
    }, 
    error: function () {
        chyba("chyba v požadavku", "df");
    }
});

if (parO2.length > 0) {
    $.ajax({
        type: "POST",
        url: "getText.asmx/ws_getText",
        data: parO2,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            /*WAIT UNTIL THE FIRST CALL IS FINISHED AND THAN DO SOMETHING*/
        },
        error: function () {
            chyba("chyba v požadavku", "df");
        }
    });
}

So any ideas? Thanks

like image 566
user1352324 Avatar asked Apr 23 '12 21:04

user1352324


People also ask

How do I make jQuery wait for an AJAX call to finish before it returns?

If you don't want the $. ajax() function to return immediately, set the async option to false : $(". my_link").

How do you check if all AJAX calls are completed?

jQuery ajaxStop() Method The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.

Can we execute run multiple AJAX requests simultaneously in jQuery?

There is a requirement to make multiple AJAX calls parallelly to fetch the required data and each successive call depends on the data fetched in its prior call. Since AJAX is asynchronous, one cannot control the order of the calls to be executed.

What is async true in AJAX call?

by default async is true. it means process will be continuing in jQuery ajax without wait of request. Async false means it will not go to next step untill the response will come.


2 Answers

If using jQuery 1.5+, you can use jQuery.when() to accomplish this. Something like (shortened the ajax calls for brevity, just pass the objects as you're doing above)

$.when($.ajax("getText.asmx/ws_getText"), 
       $.ajax("getText.asmx/ws_getText")).done(function(a1,  a2){

   // a1 and a2 are arguments resolved for the 
   // first and second ajax requests, respectively
   var jqXHR = a1[2]; // arguments are [ "success", statusText, jqXHR ]
});

You don't know in which order they will return so if you were rolling this by hand, you would need to check the state of the other request and wait until it has returned.

like image 114
Russ Cam Avatar answered Oct 11 '22 18:10

Russ Cam


You need to wire up the second call to be contained within the callback of your first ajax call. Like so:

success: function(msg)
{
    alert(msg.d.data);

    if(par02.length > 0)
    {
        // Your 2nd ajax call
    }
},

Since JavaScript doesnt run in multiple threads on the client, you can't block the thread until certain conditions are met.

like image 32
Tejs Avatar answered Oct 11 '22 19:10

Tejs