Waiting on multiple asynchronous calls to complete before continuing

You can use .ajaxStop() like this:

$(function() {
  $(document).ajaxStop(function() {
    $(this).unbind("ajaxStop"); //prevent running again when other calls finish
    LoadContact();
  });
  LoadCategories($('#Category'));
  LoadPositions($('#Position'));
  LoadDepartments($('#Department'));
});

This will run when all current requests are finished then unbind itself so it doesn’t run if future ajax calls in the page execute. Also, make sure to put it before your ajax calls, so it gets bound early enough, it’s more important with .ajaxStart(), but best practice to do it with both.

Leave a Comment