From ff79c5a3aefd263585545acfc0beffb69f65ebb1 Mon Sep 17 00:00:00 2001 From: Lukas Zahnd Date: Tue, 21 Nov 2017 17:30:27 +0100 Subject: [PATCH 1/2] Add option to delay requests by a specified amount of milliseconds, default is of course 0 if not specified --- src/jQuery.ajaxQueue.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/jQuery.ajaxQueue.js b/src/jQuery.ajaxQueue.js index 39a619b..2a7c373 100644 --- a/src/jQuery.ajaxQueue.js +++ b/src/jQuery.ajaxQueue.js @@ -10,10 +10,12 @@ $.ajaxQueue = function( ajaxOpts ) { // 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, next ); + }, ajaxOpts.delay||0); } // queue our ajax request From 84329aa01db8a1ddb0368846f397d2b8243c8758 Mon Sep 17 00:00:00 2001 From: Lukas Zahnd Date: Tue, 21 Nov 2017 17:49:38 +0100 Subject: [PATCH 2/2] Cherry pick all other merge requests from original repository and add useful functionality like handling of invalid ajaxOpts, multiple queues that can run independently, remove double next params in .then() as not needed anymore since jQuery 1.8 --- src/jQuery.ajaxQueue.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/jQuery.ajaxQueue.js b/src/jQuery.ajaxQueue.js index 2a7c373..f0e086a 100644 --- a/src/jQuery.ajaxQueue.js +++ b/src/jQuery.ajaxQueue.js @@ -1,12 +1,30 @@ (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({}, ) + 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 ) { @@ -14,7 +32,7 @@ $.ajaxQueue = function( ajaxOpts ) { jqXHR = $.ajax( ajaxOpts ); jqXHR.done( dfd.resolve ) .fail( dfd.reject ) - .then( next, next ); + .then( next ); }, ajaxOpts.delay||0); }