有沒有辦法調用jquery「when」函數中的下一個Ajax,當被調用的函數完成時(可能是回調或其他?)。 到目前爲止,我把這個例子也JSFiddle當成功回調中的單個Ajax完成時,繼續使用jQuery中的另一個函數?
陣列的URL在本例中爲本地主機履帶
var iterations = ['//localhost?p=1', '//localhost?p=2', '//localhost?p=3'];
緊緊擁抱每一個Ajax調用
var xhr = [];
這樣的迭代
$.each(iterations, function(url, k){
// store every ajax function in the array
xhr.push(
$.ajax({
url: url,
cache: false,
//async: false,
dataType: "html",
success: onSuccess
})
);
})
// then must be called when all onSuccess functions are done completed (every onsuccess have their own ajax call as well)
$.when.apply($, xhr).then(function(){
alert('This must be called when all xhrs have been triggered');
})
當這個函數裏面的AJAX調用有蜜蜂n觸發繼續下一次xhr迭代。有沒有一種方法可以對onSuccess函數進行回調?
function onSuccess(data){
$.ajax({
url: 'http://localhost',
cache: false,
//async: false,
dataType: "html",
success: function(){
alert("on success 2, continue with the next xhr iteration");
},
error: function(){
alert("on error 2, continue with the next xhr iteration");
}
})
}