【问题标题】:Multiple jQuery deferred objects in a when一个when中的多个jQuery延迟对象
【发布时间】:2014-03-09 21:02:22
【问题描述】:

那么,如果我有多个 Ajax 调用,是否每个调用都有 done 回调并在 when then? 中?

【问题讨论】:

标签: jquery promise jquery-deferred


【解决方案1】:

是的,当然可以。 done 方法甚至返回了 Promise,所以你可以简单地写

$.when(
    $.ajax(…).done(function(r) {
        console.log("ajax 1 resolved with", r)
    }),
    $.ajax(…).done(function(r) {
        console.log("ajax 2 resolved with", r)
    })
).done(function(r1s, r2s) {
    console.log("both ajax requests done");
});

【讨论】:

  • 谢谢。 Promise 和 deferred objects 需要一点时间来思考。你的回答帮助了很多
【解决方案2】:

您必须将每个 ajax 调用设置为延迟对象,然后将延迟对象设置为在 .then() 方法中解析。

【讨论】:

  • 设置为延迟对象”是什么意思?也许发布一个简短的代码示例。
猜你喜欢
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
相关资源
最近更新 更多