Code for AjaxQueue with example usage: var AjaxQueue = { batchSize: 1, //No.of simultaneous AJAX requests allowed, Default : 1 urlQueue: [], //Request URLs will be pushed into this array elementsQueue: [], //Element IDs of elements to be updated on completion of a request ( as in Ajax.Updater ) optionsQueue: [], //Request options will be pushed into this array setBatchSize: function(bSize){ //Method to set a different batch size. Recommended: Set batchSize before making requests this.batchSize = bSize; }, push: function(url, options, elementID){ //Push the request in the queue. elementID is optional and required only for Ajax.Updater calls this.urlQueue.push(url); this.optionsQueue.push(options); if(elementID!=null){ this.elementsQueue.push(elementID); } else { this.elementsQueue.push("NOTSPECIFIED"); }
this._processNext(); }, _processNext: function() { // Method for processing the requests in the queue. Private method. Don't call it explicitly if(Ajax.activeRequestCount < AjaxQueue.batchSize) // Check if the currently processing request count is less than batch size { if(AjaxQueue.elementsQueue.first()=="NOTSPECIFIED") { //Check if an elementID was specified // Call Ajax.Request if no ElementID specified //Call Ajax.Request on the first item in the queue and remove it from the queue new Ajax.Request(AjaxQueue.urlQueue.shift(), AjaxQueue.optionsQueue.shift()); var junk = AjaxQueue.elementsQueue.shift(); } else { // Call Ajax.Updater if an ElementID was specified. //Call Ajax.Updater on the first item in the queue and remove it from the queue new Ajax.Updater(AjaxQueue.elementsQueue.shift(), AjaxQueue.urlQueue.shift(), AjaxQueue.optionsQueue.shift()); } } } }; Ajax.Responders.register({ //Call AjaxQueue._processNext on completion ( success / failure) of any AJAX call. 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}); **********************************/ -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Eifion Sent: Monday, January 30, 2006 6:12 PM To: rails-spinoffs@lists.rubyonrails.org Subject: Re: [Rails-spinoffs] Multiple ajax calls Thanks. I'll have a look at this. Eifion On 30 Ion 2006, at 12:27, Irfan, Ghouseuddin Syed wrote: > I have written this small script for queuing ajax calls. Hope this > helps. > > //-------------------------------------------------------------------- > - > 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 > }); > > //-------------------------------------------------------------------- > ---- > > All the ajax calls should be pushed into the queue. You can set a > batch size ie the no.of simultaneous ajax calls to be processed. > > Usage: > Ex: AjaxQueue.setBatchSize(2); > > > > > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] Behalf Of > Eifion > Sent: Monday, January 30, 2006 5:39 PM > To: rails-spinoffs@lists.rubyonrails.org > Subject: [Rails-spinoffs] Multiple ajax calls > > > This is slightly OT for which I apologise in advance, but I was > wondering if anyone here has had any problems when making multiple > ajax calls at the same time. I'm working on a large Intranet > application which makes heavy use of ajax calls and the bugs are > flying in from the testers that if they repeatedly click on a link > that makes an ajax call then Internet Explorer can fall over. > Obviously I'm making fixes to get around this by disabling the links > while the calls are made to stop this but it seems that the way > XMLHTTP works in IE and Mozilla is quite different. If a lot of calls > are made with IE it seems to try to make them straight away which can > cause problems. FireFox on the other hand seems to wait and make the > calls sequentially which can take some time, but at least works. > > I was wondering if anyone else here had noticed anything similar and, > if so, if you'd come up with anyway of stopping IE trying to make too > many calls at once. > > Eifion > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs