我想编写一个执行十几个异步 ajax 请求的函数,等待所有这些请求完成,然后返回聚合信息。喜欢:
function countHealthy(urls) {
var healthy = 0;
for (var i = 0; i < urls.length; ++i) {
$.ajax({url: urls[i], async: true, id: i})
.done(function (data) { ++healthy; });
}
// Wait for all ajax to finish.
return healthy;
}
PS:++healthy 线程安全吗?