【发布时间】:2016-12-11 19:30:20
【问题描述】:
如何等到 ajax 在延迟函数中完成?示例:
function action() {
console.log('action is called');
var deferred = $.Deferred();
console.log('do some actions...');
//Wait until the ajax is completed and continue script
var myAjaxCall = ajaxCall();
//Execute te next scripts only after the ajax done
console.log('do some actions...');
return deferred.promise();
}
function ajaxCall() {
console.log('ajaxCall is called');
return $.ajax('url').then(function() {
console.log('success ajaxCall');
});
}
action().then(function () {
console.log('after action is done and ajaxCall is done');
});
问题是他的函数必须等到里面的ajax被调用并完成并继续其他脚本。
谢谢。
【问题讨论】:
-
你永远无法解析
action函数中的Deferred。
标签: jquery ajax deferred resolve .when