Reading a JSON-Service like this:
$.ajax({
url:'activeIDs',
success : function(data){ // data = [14,15]
var tableRows = [];
for (var dataIndex=0; dataIndex < data.length; dataIndex++) {
var isLast = dataIndex == (data.length - 1);
$.ajax({
url: 'info?id=' + data[dataIndex],
success: function(data2) { // "foo", "bar"
tableRows.push(data2.name);
if (isLast) {
alert(tableRows.length);
}
}
});
}
}
});
First network-trace is:
- activeIDs =
[14,15]
- info?id=14 (takes 2 seconds) =
"foo"
- info?id=15 (takes 4 seconds) =
"bar"
In this case the alert gives "2".
Seconds network-trace is different:
- activeIDs =
[14,15]
; - info?id=14 (takes 20 seconds) =
"foo"
(now takes very long) - info?id=15 (takes 1 second) =
"bar"
In this case the alert gives 1
after one second, this is bad!!!
Question:
How to use $.Deferred instead of isLast
?