Skip to content

Instantly share code, notes, and snippets.

@macedo
Created January 29, 2015 18:21
Show Gist options
  • Save macedo/b5f4d6b03bf02ada9182 to your computer and use it in GitHub Desktop.
Save macedo/b5f4d6b03bf02ada9182 to your computer and use it in GitHub Desktop.
Ajax requests queue
var AjaxQueue = {
batchSize: 1,
urlQueue: [],
elementsQueue: [],
optionsQueue: [],
setBatchSize: function(bSize) {
this.batchSize = bSize;
},
push: function(url, options, elementID) {
this.urlQueue.push(url);
this.optionsQueue.push(options);
if(elementID !== null){
this.elementsQueue.push(elementID);
} else {
this.elementsQueue.push("NOTSPECIFIED");
}
this._processNext();
},
_processNext: function() {
if(Ajax.activeRequestCount < AjaxQueue.batchSize) {
if(AjaxQueue.elementsQueue.first() === "NOTSPECIFIED") {
new Ajax.Request(AjaxQueue.urlQueue.shift(), AjaxQueue.optionsQueue.shift());
var junk = AjaxQueue.elementsQueue.shift();
} else {
new Ajax.Updater(AjaxQueue.elementsQueue.shift(), AjaxQueue.urlQueue.shift(), AjaxQueue.optionsQueue.shift());
}
}
}
};
Ajax.Responders.register({
onComplete: AjaxQueue._processNext
});
/************* SYNTAX ***************
AjaxQueue.setBatchSize(size);
AjaxQueue.push(URL , OPTIONS, [ElementID]);
************** USAGE ***************
AjaxQueue.setBatchSize(4);
AjaxQueue.push("http://www.testingqueue.com/process/",{onSucess: funcSuccess, onfailure: funcFailure});
AjaxQueue.push("http://www.testingqueue.com/process1/",{onSucess: funcSuccess1, onfailure: funcFailure1}, "myDiv");
AjaxQueue.push("http://www.testingqueue.com/process2/",{onSucess: funcSuccess2, onfailure: funcFailure2});
AjaxQueue.push("http://www.testingqueue.com/process3/",{onSucess: funcSuccess3, onfailure: funcFailure3});
AjaxQueue.push("http://www.testingqueue.com/process4/",{onSucess: funcSuccess4, onfailure: funcFailure4});
AjaxQueue.push("http://www.testingqueue.com/process5/",{onSucess: funcSuccess5, onfailure: funcFailure5});
**********************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment