我在这里遇到了小麻烦。在我的document.ready函数中,我定义了一个对象,并且该对象正由三个不同的ajax调用填充(在document.ready内部)。现在我想做一个
console.log(myObject);
仅当3个异步调用已完全执行时。请提出一种方法。
最佳答案
使用我建议您创建一个这样的函数:
function onAllInformationIsReady() {
console.log(myObject);
}
function isAllInformationReady() {
// verify here if you have all the information
}
并且您在ajax调用中执行了类似的操作(我不假设您在此处使用jQuery,请替换为ajax调用方法)
$.ajax({
type: "POST",
url: "some.php",
data: "...n",
success: function(msg){
if(isAllInformationReady())
onAllInformationIsReady();
}
});
顺便说一句,如果您使用的是jQuery,则可以进行如下所示的同步ajax调用:
$.ajax({
type: "POST",
url: "some.php",
data: "...n",
async: false,
success: function(msg){
}
});