Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Came across here pursuing a similar problem and decided to answer even though it's quite late for other people who might end up here with same problem.</p> <p>I believe what you need is Ajax global events. <a href="http://api.jquery.com/Ajax_Events/">See API Documentation</a></p> <p>Especially here;</p> <blockquote> <p>Global Events</p> <p>These events are triggered on the document, calling any handlers which may be listening. You can listen for these events like so:</p> </blockquote> <pre><code>$(document).bind("ajaxSend", function(){ // You should use "**ajaxStop**" instead of "ajaxComplete" if there are more // ongoing requests which are not completed yet }).bind("ajaxStop", function(){ // call your reload function here }); </code></pre> <p>Now for your case, instead of binding "ajaxComplete" event if you use "ajaxStop" this will be triggered when all Ajax requests being processed are finished.</p> <p>I copy-pasted your original code on fiddle and added the part I just recommended with some logs. <a href="http://jsfiddle.net/Tt3jk/7/">jsfiddle.net/Tt3jk/7/</a> For testing purposes I called a similar <code>SendData2()</code> function from within your first function's success event to simulate an ugly async request scenario. If you test this code on a real environment(or place the SendData2 with your url that responds with your data type which was "text" what you should see on the console is this output. (1- is console.log from <code>SendData()</code> and 2- is from <code>SendData2()</code>):</p> <pre><code>1-sending... waiting for all requests to complete... 1-success:! 2-sending... waiting for all requests to complete... 1-done: 2-success:! 2-done: completed now! </code></pre> <p>You can in fact even see it even on fiddle(with errors on the requests) when your reload function is being called. If you use "ajaxComplete", reload function inside your jQuery .click() function is being called quite early. However if you use "ajaxStop" and call reload function when "ajaxStop" event is triggered, reload function will be called after all the requests are completed.</p> <p>I don't know if fiddle disappears after a while so I will post the changes I made here as well without console logs:</p> <pre><code>$(".submit_button").click(function () { popupMessage(); sendData(); //the ajax calls are all in here // consider reloading somewhere else }); $(document).bind("ajaxSend", function () { console.log("waiting for all requests to complete..."); // ajaxStop (Global Event) // This global event is triggered if there are no more Ajax requests being processed. }).bind("ajaxStop", function () { // maybe reload here? location.reload(); }); function popupMessage() { alert("Pop!"); } function sendData() { //a bunch of these: $.ajax({ "dataType": "text", "type": "POST", "data": "temp", "url": "your url here!", "beforeSend": function (msg) { console.log("1-sending..."); }, "success": function (msg) { console.log("1-success!"); sendData2(); // again }, "error": function (msg) { console.log("1-error!"); } }).done(function (msg) { console.log("1-done!"); }); } function sendData2() { //a bunch of these: $.ajax({ "dataType": "text", "type": "POST", "data": "temp", "url": "your url here!", "beforeSend": function (msg) { console.log("2-sending..."); }, "success": function (msg) { console.log("2-success!"); }, "error": function (msg) { console.log("2-error!"); } }).done(function (msg) { console.log("2-done!"); }); } </code></pre> <p>PS. Not sure if it's a good practice or not to make another request from within a request, probably not. But I put it there to show how "ajaxStop" event is delayed to be triggered until all ongoing requests are done(or completed with error at least)...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload