Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New version with all Pull Requests unified to save you some work... #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/jQuery.ajaxQueue.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
(function($) {

// jQuery on an empty object, we are going to use this as our Queue
var ajaxQueue = $({});
var ajaxQueues = [$({})];

$.ajaxQueue = function( ajaxOpts ) {
$.ajaxQueue = function( ajaxOpts, queueIndex ) {
var jqXHR,
dfd = $.Deferred(),
promise = dfd.promise();
promise = dfd.promise(),
ajaxQueue;

// allow multiple queues that run independently of each other
// specify the queue to be used like this (index can be ommited!):
// $.ajaxQueue({<ajaxOpts>}, <int|queueIndex>)
queueIndex = queueIndex || 0; // get queue index if specified
ajaxQueue = ajaxQueues[queueIndex];

if ( ajaxQueue == undefined || !(ajaxQueue instanceof $) ) {
ajaxQueue = ajaxQueues[queueIndex] = $({});
}

// if there is no ajax request return an empty 200 code
if ( typeof ajaxOpts == "undefined" ) {
return $.Deferred(function() {
this.resolve( [ '', '200', jqXHR ] );
}).promise();
}

// run the actual query
function doRequest( next ) {
jqXHR = $.ajax( ajaxOpts );
jqXHR.done( dfd.resolve )
.fail( dfd.reject )
.then( next, next );
setTimeout(function() {
jqXHR = $.ajax( ajaxOpts );
jqXHR.done( dfd.resolve )
.fail( dfd.reject )
.then( next );
}, ajaxOpts.delay||0);
}

// queue our ajax request
Expand Down