Created
April 23, 2012 12:12
-
-
Save guybedford/2470554 to your computer and use it in GitHub Desktop.
jQuery AJAX queues
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
/* | |
Allows for ajax requests to be run synchronously in a queue | |
Usage:: | |
var queue = new $.AjaxQueue(); | |
queue.add({ | |
url: 'url', | |
complete: function() { | |
console.log('ajax completed'); | |
}, | |
_run: function(req) { | |
//special pre-processor to alter the request just before it is finally executed in the queue | |
req.url = 'changed_url' | |
} | |
}); | |
*/ | |
$.AjaxQueue = function() { | |
this.reqs = []; | |
this.requesting = false; | |
}; | |
$.AjaxQueue.prototype = { | |
add: function(req) { | |
this.reqs.push(req); | |
this.next(); | |
}, | |
next: function() { | |
if (this.reqs.length == 0) | |
return; | |
if (this.requesting == true) | |
return; | |
var req = this.reqs.splice(0, 1)[0]; | |
var complete = req.complete; | |
var self = this; | |
if (req._run) | |
req._run(req); | |
req.complete = function() { | |
if (complete) | |
complete.apply(this, arguments); | |
self.requesting = false; | |
self.next(); | |
} | |
this.requesting = true; | |
$.ajax(req); | |
} | |
}; |
I updated my own version with Cancel ,
@chandram see this can help you
https://gist.github.com/extralam/ef68e6d9bd83a68b82b3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to cancel the one from queue.
Thanks.