[英]Calling a function is JQuery after all ajax calls(number of ajax calls are dynamic) have received responses
HTML:-
結果Javascript:-
$(document).ready(function(){ $("#result").html("function started here"); var requests = Array(); requests.push($.get('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA')); requests.push($.get('https://maps.googleapis.com/maps/api/geocode/json?address=EGL, Bangalore')); var defer = $.when.apply($, requests); console.log(defer); defer.done(function(){ $("#result").html("Completed"); // This is executed only after every ajax request has been completed $.each(arguments, function(index, responseData){ // alert(index); $("#result").html(responseData); // "responseData" will contain an array of response information for each specific request }); }); });
上面的代碼段僅在所有ajax調用均返回“成功”響應時才有效。 有什么辦法可以讓我知道所有ajax調用是否都得到響應,可能是“成功” /“失敗”。
您可以ajaxStart,ajaxStop,ajaxError,ajaxSuccess。
$(document).ajaxStart(function () {
//when ajax start
}).ajaxStop(function () {
//when ajax stop
}).ajaxError(function () {
//when ajax error
}).ajaxSuccess(function () {
// when ajax success
});
這將為每個ajax調用觸發。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.