jQuery的AJAX回调推迟订单 [英] jQuery ajax deferred callback orders

查看:161
本文介绍了jQuery的AJAX回调推迟订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法得到完成/失败/回调总是在我的每一个Ajax请求延期对象的回调之前执行的。

I cannot get the done/fail/always callbacks on each of my ajax requests to execute before the deferred object's callbacks.

我的问题是,我的一些Ajax请求可能会失败,但我需要执行相同的code一个人是否失败或没有失败。

The problem I have is that some of my ajax requests may fail, but I need to execute the same code whether one fails or none fail.

很难准确解释,所以我做了这个小提琴,以帮助证明我有这个问题。 http://jsfiddle.net/zZsxV/

Hard to explain exactly, so I made this fiddle to help show the problem I am having. http://jsfiddle.net/zZsxV/

var a1 = $.Deferred();
var a2 = $.Deferred();
var a3 = $.Deferred();

a1.done(function() {
    $('body').append('a1 done<br />');     
}).fail(function() {
    $('body').append('a1 fail<br />');     
}).always(function() {
    $('body').append('a1 always<br />');     
});

a2.done(function() {
    $('body').append('a2 done<br />');     
}).fail(function() {
    $('body').append('a2 fail<br />');     
}).always(function() {
    $('body').append('a2 always<br />');     
});

a3.done(function() {
    $('body').append('a3 done<br />');     
}).fail(function() {
    $('body').append('a3 fail<br />');     
}).always(function() {
    $('body').append('a3 always<br />');     
});

var def = $.when(a1, a2, a3);
def.always(function() {
    $('body').append('defer always <-- should be after all<br />'); 
});

setTimeout(function() {
    a1.resolve();
}, 5000);
setTimeout(function() {
    a2.reject();
}, 1000);
setTimeout(function() {
    a3.resolve();
}, 3000);

我读了很多关于这一主题的答案,但我不相信任何正好符合我的需要。

I read a lot of the answers on this topic but I don't believe any fit my need exactly.

需要帮助,请让我知道,我会加一次我回来的任何详细信息。先谢谢了。

Any more information needed to help please let me know and I'll add it once I get back. Thanks in advance.

我不明白发生了什么。我只知道如何做到这一点,以避免这个问题。我尝试使用。那么,以及具有相同的结果。一旦一个请求被拒绝了,它会等待对方回调之前失败的回调。

I do understand what is happening. I just know how to do it to avoid this problem. I tried using .then as well with the same results. Once one request is rejected, it fires the fail callback before waiting for the other callbacks.

推荐答案

根据 jQuery的文档 $。当触发回调时,无论所有参数得到解决或一个被拒绝。因此,如何利用类似的东西,而不是 $当

According to jQuery documentation $.when fires callbacks when either all of parameters are resolved or one of them is rejected. So how about using something like that instead of $.when:

var custom_when = function() {
    var args = $.makeArray(arguments);
    var callback = args.pop();
    var all = [];
    $.each(args, function(index, def) {
        def.always(function() {
            var idx = all.indexOf(def);
            if (idx !== -1) {
                all.splice(idx, 1);
            }
            if (!all.length) {
                callback();
            }
        });
        all.push(def);
    });
};

和您可以使用它这样的:

and you can use it like that:

custom_when(a1, a2, a3, function() {
    // do some stuff.
});

的jsfiddle演示

这篇关于jQuery的AJAX回调推迟订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆