2

我有如下示例。我总是在“a”和“b”之前先得到“c”。我如何相应地得到“a”、“b”和“c”?我将不胜感激任何建议。

b.extend({
        get: function (id) {

        jQuery.ajax({
            type: 'GET',
            url: url,
            data: pdata,
            success: function (result) {
                console.log("a");
            }
        });

        for (var a = 0; a < 5; a++) {
            jQuery.ajax({
                type: 'GET',
                url: url,
                data: pdata,
                success: function (result) {
                    console.log("b");
                }
            });
        }
        console.log("c");
        }
});
4

4 回答 4

6

尝试

把你的代码放进去success:

b.extend({
    get: function (id) {
        jQuery.ajax({
            type: 'GET',
            url: url,
            data: pdata,
            success: function (result) {
                console.log("a");
                for (var a = 0; a < 5; a++) {
                    jQuery.ajax({
                        type: 'GET',
                        url: url,
                        data: pdata,
                        success: function (result) {
                            console.log("b");
                            if (a === 5) {
                                console.log("c");
                            }
                        }
                    });
                }
            }
        });
    }
});
于 2013-11-19T14:03:30.367 回答
2

您还可以使用deferred

b.extend({
    get: function (id) {

        var request = jQuery.ajax({
            type: 'GET',
            url: url,
            data: pdata
        }).then(function(result) {
            console.log("a");
            return result;
        });

        for (var a = 0; a < 5; a++) {
            request = request.then(function(result) {
                return jQuery.ajax({
                    type: 'GET',
                    url: url,
                    data: pdata
                }).then(function(result) {
                    console.log("b");
                    return result;
                });
            });
        }

        request.then(function() {
            console.log("c");
        });
    }
});
于 2013-11-19T14:11:17.037 回答
0

在B的回调中调用C,在A的回调中调用B

于 2013-11-19T14:03:34.877 回答
0

jQuery 是异步的,因此某些 Ajax 请求可能会先于其他请求完成。

b.extend({
    get: function (id) {

    var async = $.ajaxSetup()['async']; // store the current value of async to a variable

    $.ajaxSetup({'async':false}); // Set async to false

    jQuery.ajax({
        type: 'GET',
        url: url,
        data: pdata,
        success: function (result) {
            console.log("a");
        }
    });

    for (var a = 0; a < 5; a++) {
        jQuery.ajax({
            type: 'GET',
            url: url,
            data: pdata,
            success: function (result) {
                console.log("b");
            }
        });
    }
    console.log("c");

    $.ajaxSetup({'async': async }); // Set async to back to original value

    }
});

唯一的缺点是您的页面将“挂起”直到请求完成

于 2013-11-19T14:05:31.220 回答