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, 2022 in Java by Edureka
• 13,670 points
14,886 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, 2022 by gaurav
• 23,260 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,230 points
869 views
0 votes
1 answer

jQuery AJAX submit form

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

answered Feb 8, 2022 in Java by Soham
• 9,700 points
1,290 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,910 points
1,670 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,370 points
928 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,320 points
885 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,320 points
660 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,910 points
1,649 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, 2022 in JQuery by gaurav
• 23,260 points
529 views
0 votes
1 answer

Google AJAX Libraries CDN for jQuery

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

answered May 30, 2022 in Java by gaurav
• 23,260 points
1,144 views
0 votes
1 answer

jQuery.click() vs onClick

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

answered May 30, 2022 in Java by gaurav
• 23,260 points
4,237 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP