Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery$.when$。然后发出_Jquery_Jquery Deferred - Fatal编程技术网

jquery$.when$。然后发出

jquery$.when$。然后发出,jquery,jquery-deferred,Jquery,Jquery Deferred,我尝试使用$。当使用ajax调用时,如果不总是调用其中的一个调用,我如何实现这一点。我一直在设法绕过它 var renderThis; $.when($.post("/blabla", function(data){ renderThis = data; }), function(){ if(another){ return $.post("/blabla");

我尝试使用
$。当
使用ajax调用时,如果不总是调用其中的一个调用,我如何实现这一点。我一直在设法绕过它

var renderThis;
$.when($.post("/blabla", function(data){
            renderThis = data;
        }),
        function(){
            if(another){
                return $.post("/blabla");
            }
            else{
                return true;
            }
        })
        .then(function(){
            render(renderThis)
        });
但我看到的是,
$.then()
不是以延迟方式调用的,而是立即调用的

有什么想法吗?

试试看

var requests = [$.post("/blabla")];
if (another) {
    requests.push($.post("/blabla"))
}
$.when.apply($, requests).then(function (data1) {
    render(data1[0])
});

。当
需要函数结果而不是函数引用时。当是函数引用时,
的第二个参数。请尝试改用立即执行的函数:

$.when(
    $.post("/blabla"),
    (function() {
        if(another){
            return $.post("/blabla");
        }
        else{
            return true;
        }
    })())
.then(
    function(data1, data2){
        render(data1);
    });
正如您所见,我还整合了第一个
success
函数,在
时处理
,而不是在
调用中提供它