I have to make N ajax requests. Each $.ajax call returns a Deferred, but also immediately performs the ajax request. What i want to do is get the Deferreds for all N requests, but only have them performed gradually (say through a setInterval loop). Is it possible to do this?
Asked
Active
Viewed 71 times
1 Answers
1
The browser will by itself limit the number of parallel ajax requests made to a single server (e.g. 4 or 8 at the same time).
If the limit it's not enough, you could make a queue and schedule some queries:
var queue = [];
queue.push(function() {
return $.ajax(...);
});
queue.push(function() {
return $.ajax(...);
});
...
Then run e.g. 2 of the queries, and each time one finishes, take an other from the queue and run it:
function runNext() {
var fun = queue.shift();
if (fun) {
fun().always(runNext);
}
}
for (var i = 0; i < 2; ++i) {
runNext();
}

Arnaud Le Blanc
- 98,321
- 23
- 206
- 194
-
thanks, i ended up doing something like this (with timeout between calls to runNext). i just thought there might be a way for Deferred's to support this without additional code. – v_y Jul 05 '12 at 18:56