Skip to content

Instantly share code, notes, and snippets.

@guybedford
Created April 23, 2012 12:12
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save guybedford/2470554 to your computer and use it in GitHub Desktop.
Save guybedford/2470554 to your computer and use it in GitHub Desktop.
jQuery AJAX queues
/*
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);
}
};
@chandram
Copy link

How to cancel the one from queue.

Thanks.

@extralam
Copy link

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