做一些如下:
function loadArray(array, batchSize, callback) {
var length = array.length
batchSize = length < batchSize ? length : batchSize; // minimum
var batch = array.slice(0, batchSize); // the current batch
array = array.slice(batchSize); // the rest of the array
loadBatch(batch, function (results) {
if (array.length) { // there are more batches to process
// load the rest of the array
loadArray(array, batchSize, function (nextResults) {
// merge the results - recursion handles the rest
callback(results.concat(nextResults));
});
} else callback(results); // last batch
});
}
的loadBatch
功能如下:
function loadBatch(batch, callback) {
var completed = 0;
var length = batch.length;
var results = new Array(length); // the array of results
$.each(array, function (index, element) {
$.ajax(element, {
complete: function (xhr, status) {
// save the results
results[index] = {
xhr: xhr,
status: status
};
if (++completed === length) callback(results); // done
}
});
});
}
現在,您可以按如下加載你的資源:
loadArray(["a.json", "b.txt", ...], 10, function (results) {
var a = JSON.parse(results[0]);
var b = results[1];
// and so on
});
這它。告訴我,如果你有任何問題。
http://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-request-are-done – Andreas