/* Allows for ajax requests to be run synchronously in a queue with pool , without cancel Usage:: var queue = new $.AjaxQueue(); // add with tag name or not , both ok queue.add('test',{ 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.MAX_POOL_SIZE = 3; this.CURR_POOL_SIZE = 0; this.reqs = []; }; $.AjaxQueue.prototype = { add: function(req) { //console.log("Add Request : "); this.reqs.push({n : '' , r: req }); this.next(); }, add: function(name , req) { console.log("Add Request : " + name); this.reqs.push( {n : name , r: req }); this.next(); }, remove: function (name){ // TODO: NOT IMPLEMENT }, next: function() { console.log(' Q Size ' + this.reqs.length); if (this.reqs.length == 0) { return; } if(this.CURR_POOL_SIZE >= this.MAX_POOL_SIZE){ return; } var request = this.reqs.splice(0, 1)[0]; var req = request['r']; var complete = req.complete; var self = this; self.current_reqs_tag = request['n']; if (req._run) { req._run(req); } req.complete = function() { if (complete) { complete.apply(this, arguments); } self.CURR_POOL_SIZE--; self.next(); } self.CURR_POOL_SIZE++; self.current_req = $.ajax(req); } };