在这个问题上真的很难,而且我知道$.when()可以这样使用(有多个AJAX语句)来保证你完成所有内容.
$.when(
$.ajax({ url: '/echo/html/', success: function(data) {
alert('request 1 complete')
}
}),
$.ajax({ url: '/echo/html/', success: function(data) {
alert('request 2 complete')
}
})
).then( function () { alert('all complete'); });
但这只适用于原始的$.ajax(),无论如何都有与函数调用相同的功能,反过来又在它们内部(和其他随机逻辑)中有ajax?
伪代码的想法:
// The functions having the AJAX inside them of course
$.when(ajaxFunctionOne, ajaxFunctionTwo).then(function () {
alert('all complete');
});
解决方法:
function ajaxFunctionOne() {
return $.ajax(...)
}
function ajaxFunctionTwo() {
var dfd = $.Deferred();
// on some async condition such as dom ready:
$(dfd.resolve);
return dfd.promise();
}
function ajaxFunctionThree() {
// two ajax, one that depends on another
return $.ajax(...).then(function(){
return $.ajax(...);
});
}
$.when(ajaxFunctionOne(),ajaxFunctionTwo(),ajaxFunctionThree()).done(function(){
alert("all complete")
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。