1
|
var AjaxQueue = {
|
2
|
batchSize: 1, //No.of simultaneous AJAX requests allowed, Default : 1
|
3
|
urlQueue: [], //Request URLs will be pushed into this array
|
4
|
elementsQueue: [], //Element IDs of elements to be updated on completion of a request
|
5
|
optionsQueue: [], //Request options will be pushed into this array
|
6
|
currentRequest: null,
|
7
|
setBatchSize: function(bSize){ //Method to set a different batch size. Recommended: Set batchSize before making requests
|
8
|
this.batchSize = bSize;
|
9
|
},
|
10
|
push: function(url, options, elementID){ //Push the request in the queue. elementID is optional and required only for Ajax requests that updates the element
|
11
|
this.urlQueue.push(url);
|
12
|
this.optionsQueue.push(options);
|
13
|
if(elementID!=null){
|
14
|
this.elementsQueue.push(elementID);
|
15
|
} else {
|
16
|
this.elementsQueue.push("NOTSPECIFIED");
|
17
|
}
|
18
|
|
19
|
this._processNext();
|
20
|
},
|
21
|
_processNext: function() { // Method for processing the requests in the queue. Private method. Don't call it explicitly
|
22
|
if(this.currentRequest == null && this.urlQueue.length > 0) // Check if the currently processing request count is less than batch size
|
23
|
{
|
24
|
// Call jQuery.ajax on the first item in the queue and remove it from the queue
|
25
|
AjaxQueue.currentRequest = jQuery.ajax(AjaxQueue.urlQueue.shift(), AjaxQueue.optionsQueue.shift());
|
26
|
AjaxQueue.currentRequest.complete( function() {
|
27
|
//Call AjaxQueue._processNext on completion ( success / failure) of this AJAX request.
|
28
|
AjaxQueue.currentRequest = null;
|
29
|
AjaxQueue._processNext();
|
30
|
});
|
31
|
if(this.elementsQueue[0]=="NOTSPECIFIED") { //Check if an elementID was specified
|
32
|
// If no ElementID was specified remove the first item from the queue
|
33
|
var junk = AjaxQueue.elementsQueue.shift();
|
34
|
} else {
|
35
|
// If ElementID was specified update the first item in the queue and remove it from the queue
|
36
|
AjaxQueue.currentRequest.success( function(data) {
|
37
|
jQuery(AjaxQueue.elementsQueue.shift()).html(data);
|
38
|
});
|
39
|
}
|
40
|
}
|
41
|
}
|
42
|
};
|
43
|
|