我尝试将 $.when
与 ajax 调用一起使用,其中的调用并不总是被调用,我该如何实现这一点。我一直在尝试破解它
var renderThis;
$.when($.post("/blabla", function(data){
renderThis = data;
}),
function(){
if(another){
return $.post("/blabla");
}
else{
return true;
}
})
.then(function(){
render(renderThis)
});
但我看到的是 $.then()
不是以延迟方式调用,而是立即调用。
有什么想法吗?
最佳答案
.when
需要函数结果而不是函数引用。 .when
的第二个参数是函数引用。尝试使用立即执行的函数:
$.when(
$.post("/blabla"),
(function() {
if(another){
return $.post("/blabla");
}
else{
return true;
}
})())
.then(
function(data1, data2){
render(data1);
});
如您所见,我还整合了第一个 success
函数,以便在 when
上进行处理,而不是将其与 .post
调用内联提供。
关于jquery $.when $.then 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19787614/