here is the code;
var jobQueue = [];
var ajaxActive = false;
function addToQueue(url, data) {
jobQueue.push({'url' : url, 'data': data});
doAjax();
}
function doAjax() {
if (ajaxActive) { return; } // ajax request still outstanding
if (jobQueue.length = 0) { return; } // no more work to do
todo = jobQueue.pop();
ajaxActive = true;
$.ajax({
url: todo.url,
data: todo.data,
success: function(returnedata) {
... do whatever you need with the returned data ...
ajaxActive = false;
doAjax(); // schedule another job, if need be.
},
error: function(e) {
... handle error ...
ajaxActive = false;
doAjax(); // schedule next job, if need be
}
});
}