3 回答

TA貢獻1788條經驗 獲得超4個贊
簡單的方法
最簡單的方法是使用.ajaxStop()事件處理程序:
$(document).ajaxStop(function() {
// place code to be executed on completion of last outstanding ajax call here
});
艱難的道路
您還可以手動檢測是否有任何ajax調用仍處于活動狀態:
創建一個包含活動Ajax連接數的變量:
var activeAjaxConnections = 0;
在打開新的Ajax連接之前,遞增該變量
$.ajax({
beforeSend: function(xhr) {
activeAjaxConnections++;
},
url (...)
在success部分檢查,如果該變量等于零(如果是的話,最后的連接已完成)
success: function(html){
activeAjaxConnections--;
$('#result_'+rowNum).empty().append(html);
$('#coda_'+rowNum).removeClass("loading");
$('#coda_'+rowNum).addClass("loader");
if (0 == activeAjaxConnections) {
// this was the last Ajax connection, do the thing
}
},
error: function(xhr, errDesc, exception) {
activeAjaxConnections--;
if (0 == activeAjaxConnections) {
// this was the last Ajax connection, do the thing
}
}
如您所見,我還添加了檢查返回錯誤的信息
- 3 回答
- 0 關注
- 637 瀏覽
添加回答
舉報