JavaScript jQuery - completion of multiple ajax-requests

in a $.each() I do a AJAX-request:

$.each(all, function(i,v) {                                 
    $.ajax({
        url: "/mycontroller/"+encodeURIComponent(v),
        success: function(data){ 
            $('#inner').append(data);
        }
    }); 
});

now I would like to show a message if every AJAX-request in the $.each() is complete. But how can I do this, As AJAX is asynchronous?

Answer:1

With simple javascript you can do it in following way:

var counter = 0;
$.each(all, function(i,v) {                                 
    $.ajax({
        url: "/mycontroller/"+encodeURIComponent(v),
        success: function(data){ 
            $('#inner').append(data);
            counter++; //increment the counter
        },
        error: function(){
            counter++; //increment the counter
        },
        complete : function(){
            //check whether all requests been processed or not
            if(counter == all.length)
            {
                alert("All request processed");
            }
        }
    }); 

});
Answer:2

You can utilize jQuery.when(). This method

provides a way to execute callback functions based on zero or more objects, usually Deferred objects that represent asynchronous events.

var ajaxRequests = all.map(function(x) { 
    return $.ajax({
        url: "/mycontroller/"+encodeURIComponent(x),
        success: function(data){ 
            $('#inner').append(data);
    }
}); 
jQuery.when.apply(this, ajaxRequests).then(function() {
    // do what you want
});
Answer:3

I have a modal form using bootstrap. The form contains some text inputs and a image input. I submit the form with ajax, and all data is received at the PHP file correctly. Alas, the image isn't ...

I have a modal form using bootstrap. The form contains some text inputs and a image input. I submit the form with ajax, and all data is received at the PHP file correctly. Alas, the image isn't ...

  1. submit form with javascript
  2. submit form with jquery
  3. submit form with ajax
  4. submit form with button
  5. submit form with link
  6. submit form with anchor
  7. submit form with a tag
  8. submit form with button outside form
  9. submit form with enter key
  10. submit form with get method
  11. submit form with php
  12. submit form with react
  13. submit form with input type button
  14. submit form with fetch
  15. submit form with curl
  16. submit form with selenium python
  17. submit form with enter key react
  18. submit form with value javascript
  19. submit form with multiple inputs
  20. submit form with python requests

Basically I'm trying to copy data from <input>s on one tab and then paste it into a form <input>s on another tab using a chrome extension. The part that I'm having trouble with figuring ...

Basically I'm trying to copy data from <input>s on one tab and then paste it into a form <input>s on another tab using a chrome extension. The part that I'm having trouble with figuring ...

I'v been following a few walkthroughs on how to implement d3 charts in an angular application. Basically, Im trying to implement the following d3 chart into my custom angular directive ('workHistory')....

I'v been following a few walkthroughs on how to implement d3 charts in an angular application. Basically, Im trying to implement the following d3 chart into my custom angular directive ('workHistory')....

I am using D3 v4. I have a bar graph created with an x-axis using scaleBand(). Now, I have created a y-axis but my issue is that no matter how I position it, it is cutting into the actual bars of ...

I am using D3 v4. I have a bar graph created with an x-axis using scaleBand(). Now, I have created a y-axis but my issue is that no matter how I position it, it is cutting into the actual bars of ...