-
-
Save pedrosland/1438613 to your computer and use it in GitHub Desktop.
jQuery.ajaxQueue - A queue for ajax requests
This file contains 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.ajaxQueue - A queue for ajax requests | |
* | |
* (c) 2011 Pedro Sland | |
* Original by (c) 2011 Corey Frang | |
* | |
* Dual licensed under the MIT and GPL licenses. | |
* | |
* Requires jQuery 1.5+ | |
*/ | |
(function($) { | |
// Object for namespaced queues | |
window.ajaxQueue = {}; | |
$.ajaxQueue = function( ajaxOpts ) { | |
var jqXHR, | |
dfd = $.Deferred(), | |
promise = dfd.promise(), | |
queue = ajaxOpts.queue || 'default'; | |
if(!ajaxQueue[queue]){ | |
ajaxQueue[queue] = []; | |
} | |
// queue our ajax request | |
ajaxQueue[queue].push(doRequest); | |
// add the abort method | |
promise.abort = function( statusText ) { | |
var ret = promise; | |
// proxy abort to the jqXHR if it is active | |
if ( jqXHR ) { | |
ret = jqXHR.abort( statusText ); | |
} | |
var index = $.inArray( doRequest, ajaxQueue[queue] ); | |
if ( index > -1 ) { | |
ajaxQueue[queue].splice( index, 1 ); | |
} | |
// and then reject the deferred | |
dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] ); | |
return promise; | |
}; | |
// run the actual query | |
function doRequest() { | |
jqXHR = $.ajax( ajaxOpts ) | |
.done( dfd.resolve ) | |
.fail( dfd.reject ) | |
.always( doNext ); | |
} | |
// Remove the current item and run the next if it exists. | |
function doNext(){ | |
ajaxQueue[queue].shift(); | |
if(ajaxQueue[queue][0]){ | |
ajaxQueue[queue][0](); | |
} | |
} | |
if(ajaxQueue[queue].length == 1){ | |
doRequest(); | |
} | |
return promise; | |
}; | |
})(jQuery); |
This file contains 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.ajaxQueue - A queue for ajax requests | |
* | |
* (c) 2011 Pedro Sland | |
* Original by (c) 2011 Corey Frang | |
* | |
* Dual licensed under the MIT and GPL licenses. | |
* | |
* Requires jQuery 1.5+ | |
*/ | |
(function(c){window.a={};c.a=function(d){function g(){h=c.c(d).f(e.m).g(e.k).d(k)}function k(){ajaxQueue[b].shift();if(ajaxQueue[b][0])ajaxQueue[b][0]()}var h,e=c.b(),f=e.i(),b=d.j||"default";ajaxQueue[b]||(ajaxQueue[b]=[]);ajaxQueue[b].push(g);f.abort=function(i){h&&h.abort(i);var j=c.h(g,ajaxQueue[b]);-1<j&&ajaxQueue[b].splice(j,1);e.l(d.e||d,[f,i,""]);return f};1==ajaxQueue[b].length&&g();return f}})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment