我需要計算jQuery ajax調用在列表中的每個項目上成功執行的次數。jQuery:在每個項目中成功執行jQuery ajax調用的次數.each()
我從這裏開始,但隨後意識到了這一點並不會因爲AJAX的「異步」部分的工作:
var numSuccessfullUpdates = 0;
$('#saveMyListItems').click(function (event) {
event.preventDefault();
$('ul#myList li').each(function() {
$.ajax({
url: '[my_url]',
type: "POST",
data: {
// [data block here, not relevant to question]
},
success: function() {
numSuccessfullUpdates++;
}
});
});
if (successfullUpdates > 0){
alert(numSuccessfullUpdates);
}
});
有什麼建議?
您是否意識到或測試過? – 2011-06-16 07:10:08
你想等到他們全部完成嗎?在超時時間到期之前? – justis 2011-06-16 07:10:17
@編碼怪胎:我測試過,然後實現。 – Faust 2011-06-16 07:11:41