I have a page in which I have a variable number of AJAX calls to create that fire on a general event. AJAX calls themselves are updating related fields in an SQL database. When all the calls are completed, I want to refresh the page so that it now reflects the changes just made.
I used the following question / answer to get so far .. jQuery, when each one is complete, the trigger function
Here is my code:
var team_update = function(e) { var $items = $('.sortable-items'); var XHRs = []; $items.each(function(){ var team_id = $( this ).attr("id"); var data = { tid: team_id, users: $('#' + team_id).sortable('toArray') }; XHRs.push( $.ajax({ type: "POST", url: "update.php", data: data, complete: function(data){ } }) ); }); $.when(XHRs).then(function(){ console.log('Refreshing Screen'); $('#content-wrapper').load( 'index.php' ); }); }
What I was expecting is that $('#content-wrapper').load( 'index.php' );
will fire as soon as all my ajax () requests are complete. What seems to be happening is that the callback starts after all requests have been sent, and not necessarily after they are completed, and therefore sometimes updating my page still contains "old" data.
The graph below shows the initial loading of the page at the top, which can be ignored. His next 4 entries that show the problem. There are 3 POST requests, which are my 3 ajax calls to update the database, and the final GET, which is the page update. A GET page refresh is triggered after all 3 ajax calls have been sent, but it does not wait for the last ajax call to complete before it starts. As a result, it receives the old data as it completes before the previous ajac call completed the database update.

What am I doing wrong here?
Boidy source share