05-12-2010

Javascript simple Ajax queuing script

The folowing javascript places Ajax calls in an array and goes through them one by one making sure they get called successively and only one at the time, allowing you to controll the order in which your calls reach / are processed by the server.

In this example the $.post() method calls made presume the jQuery library is loaded. To adapt to other library just use another ajax call method.

var aQueue = [];
var processing = false; 	 
var ajaxQueue = function(urlString,varsObject){
	aQueue.push({"u":urlString,"vars":varsObject});
 
	if (processing) return; 
 
		ajaxQueueProcess();
	}
 
var ajaxQueueProcess = function(){
	if (aQueue.length){
		processing = true; 
		var currentRequest = aQueue.shift();
		$.post(currentRequest['u'],currentRequest['vars'],ajaxQueueProcess);
	}else{
		processing = false;
	}	
}

Comments:

Your comment:

»

 

[x]