Skip to content

Instantly share code, notes, and snippets.

@justinobney
Created February 28, 2012 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinobney/1933539 to your computer and use it in GitHub Desktop.
Save justinobney/1933539 to your computer and use it in GitHub Desktop.
Wait for a set of AJAX calls to finish before running a callback
/*
* jQuery executeAfter extention
* stole this from: [CHAD] http://stackoverflow.com/users/54746/chad
* SO URL: http://stackoverflow.com/questions/2768293/waiting-on-multiple-asynchronous-calls-to-complete-before-continuing
*/
;(function($) {
$.fn.executeAfter = function(methods, callback) {
var stack = [];
var trackAjaxSend = function(event, XMLHttpRequest, ajaxOptions) {
var url = ajaxOptions.url;
stack.push(url);
}
var trackAjaxComplete = function(event, XMLHttpRequest, ajaxOptions) {
var url = ajaxOptions.url;
var index = jQuery.inArray(url, stack);
if (index >= 0) {
stack.splice(index, 1);
}
if (stack.length == 0) {
callback();
$this.unbind("ajaxComplete");
}
}
var $this = $(document);
$this.ajaxSend(trackAjaxSend)
$this.ajaxComplete(trackAjaxComplete)
methods();
$this.unbind("ajaxSend");
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment