views:

28

answers:

1

The first time a user is visiting my website, I am pulling a lot of information from various sources using a couple of ajax calls. How do I reload the page once the ajax calls are done?

if(userVisit != 1) {
  // First time visitor
  populateData();
}

function populateData() {
  $.ajax({
            url: "server.php",
            data: "action=prepare&myid=" + id,
            dataType: "json",
            success: function(json) {
                if(json.error) { 
                    return;
                }
                _id = response[json].id;
                getInformation(_id);
            }
  });
}

function getInformation(id) {
  $.ajax({
            url: "REMOTESERVICE",
            data: "action=get&id=" + id,
            dataType: "json",
            success: function(json) {
                if(json.error) { 
                    return;
                }

                $.ajax({
                       url: "server.php",
                       data: "action=update&myid=" + id + '&data=' + json.data.toString(),
                       dataType: "json",
                       success: function(json) {
                                if(json.error) { 
                                    return;
                                }
                      }
                });
            }
  });
}

So what the code does is, it gets a list of predefined identifiers for a new user (populateData function) and uses them to get more information from a thirdparty service (getInformation function). This getInformation function queries a third party server and once the server returns some data, it sends that data to my server through another ajax call. Now what I need is a way to figure out when all the ajax calls have been completed so that I can reload the page. Any suggestions?

+3  A: 

In your getInformation() call you can call location.reload() in your success callback, like this:

success: function(json) {
  if(!json.error) location.reload(true);
}

To wait until any further ajax calls complete, you can use the ajaxStop event, like this:

success: function(json) {
  if(json.error) return;
  //fire off other ajax calls
  $(document).ajaxStop(function() { location.reload(true); });
}
Nick Craver
@Nick Craver: Thanks. But what if some ajax calls to my server inside the `success` callback in the `getInformation` function are still pending? Wouldn't this just refresh the page without considering that? Or am I missing something?
Legend
@Legend - Yes, if you want to delay until they're all done, just do `$(document).ajaxStop(function() { location.reload(true); });` and it'll execute when all those requests complete.
Nick Craver
@Nick Craver: That makes total sense but for some reason, when I insert this call, the page keep refreshing all the time.
Legend
@Legend - Are you running these functions even after the user data has been loaded? If that's the case it'll keep reloading like that.
Nick Craver
@Nick Craver: You are right. I fixed it. Thanks a lot. Works perfectly... Just one thing though... It works in Chrome but in Firefox, it keeps restarting like before...
Legend