Wait until all jQuery Ajax requests are done

0 votes
How do I make a function wait until all jQuery Ajax requests are done inside another function?

In short, I need to wait for all Ajax requests to be done before I execute the next. But how?
May 27 in Java by Edureka
• 13,640 points
151 views

1 answer to this question.

0 votes

You may need to run certain code only after all Ajax requests have completed when working with many Ajax requests. To do this, use the when function in jQuery.
$.when in JQuery
It takes any number of arguments and executes once all of the parameter functions have been performed.

If you have three Ajax requests and wish to wait for all three to complete, use the code below:

$.when(ajax1(), ajax2(), ajax3()).done(function(resp1, resp2, resp3) {
    // the code here will be executed when all four ajax requests resolve.
    console.log('All 3 ajax request complete.');
});

function ajax1() {
    return $.ajax({
        url: url1,
        dataType: "json",
        data: jsonRequestData1,
        ...
    });
}

function ajax2() {
    return $.ajax({
        url: url2,
        dataType: "json",
        data: jsonRequestData2,
        ...
    });
}

function ajax3() {
    return $.ajax({
        url: url3,
        dataType: "json",
        data: jsonRequestData3,
        ...
    });
}
answered May 30 by gaurav
• 17,000 points

Related Questions In Java

0 votes
2 answers

What are all the different ways to create an object in Java?

There are different ways you could do this ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,170 points
443 views
0 votes
1 answer

jQuery AJAX submit form

There is a simple input mentioned below ...READ MORE

answered Feb 8 in Java by Soham
• 8,740 points
237 views
+4 votes
3 answers

What are the steps to set Java_home on Windows?

Set Java Home JAVA_HOME = C:\Program Files\Java\jdk1.7.0 [Location ...READ MORE

answered Sep 18, 2018 in Java by Sushmita
• 6,900 points
541 views
0 votes
1 answer

what are the ways in which a list can be iterated

  There are 5 ways to iterate over ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,350 points
410 views
0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 26, 2018 in Java by developer_1
• 3,310 points
355 views
0 votes
1 answer

What are optional parameters in Java

Using three dots: public void move(Object... x) { ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,310 points
378 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,900 points
1,076 views
0 votes
1 answer

What is the difference between AJAX with JavaScript and jQuery?

JavaScript is a programming language. JQuery is ...READ MORE

answered May 30 in JQuery by gaurav
• 17,000 points
62 views
0 votes
1 answer

Google AJAX Libraries CDN for jQuery

The Google Hosted Libraries is a stable, ...READ MORE

answered May 30 in Java by gaurav
• 17,000 points
210 views
0 votes
1 answer

jQuery.click() vs onClick

So, using a string that is tied ...READ MORE

answered May 30 in Java by gaurav
• 17,000 points
45 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP