【问题标题】:Callback of deferred to be executed while promise is pending在 promise 未决时执行的 deferred 回调
【发布时间】:2014-06-04 19:02:00
【问题描述】:

如何配置代码以在等待jQuery.Deferred 的承诺时运行?比如:

$.when(someAJAX).whatever(function() {
    // run this while the AJAX is loading
}).done(function() {
    // run this when it is done
});

我检查了the docs,但一无所获,但也许我遗漏了一些东西(deferred.progress() 在这里不起作用)。

【问题讨论】:

    标签: javascript jquery ajax jquery-deferred


    【解决方案1】:

    我认为你想要的比你想要做的要简单得多。相反,您可以这样做:

    $.when.apply($, arrayOfPromises).done(function() {
        // code here when ajax call is done
    });
    // this code will execute right after the ajax call was initiated
    // put code here to set state while ajax is loading
    

    【讨论】:

    • 啊,这很聪明。谢谢!顺便说一句,我使用$.when() 的原因是因为我有一系列承诺(所以我的代码中实际上有$.when.apply(promises)...)。我只是决定在这里提供一个简化模型:)
    • @Bluefire - 好的,那么您可以使用$.when(array of promises) 代替我的答案中的$.ajax()
    • $.when.apply(null, promises) 我希望。
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2020-11-24
    • 2016-04-26
    • 2018-05-22
    • 2019-04-17
    • 2017-10-28
    相关资源
    最近更新 更多