在一个when中包含多个jQuery延迟对象



所以,如果我有多个Ajax调用,是否有可能为他们每个有done回调,并在when then? ?

是的,当然有可能。done方法甚至返回承诺,因此您可以简单地写入

$.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");
});

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

最新更新