Created
February 28, 2012 16:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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