I have an array of data (arrData) which I loop around (10 elements), calling an ajax request on each. As it stands at the moment each ajax call is made in parallel. This means the server receives 10 calls all at once and sends back the data in whatever order it calculates each in. To relieve the server slightly I want to now queue my calls. Therefore, when one set of data is received by ajax it calls the next lookup. Is there any way method of doing this built in to jquery or should I hack my loop and add a recursive callback to each?
$.each(arrData, function(k,v) {
$.getJSON(lookupPath,
{ key:k, val:v },
function(data) {
console.log(data); //loopback here?
}
);
});
UPDATE: I went with the following (as suggested by Scott M.) to get this done:
function doLookup() {
if(k < arrData.length) {
var v = arrData[k];
$.getJSON(lookupPath,
{ key:k, val:v },
function(data) {
console.log(data);
k++;
doLookup();
}
);
}
}
var k = 0;
doLookup();