小编Fra*_*rez的帖子

内部循环中的Ajax调用需要顺序响应

我需要进行3次或更少的ajax调用,并且响应需要按照请求的顺序附加到dom.

我有以下功能,但问题是我得到的响应在被附加到dom时不一定是正确的顺序.

我不想使用async:false属性,因为它会阻止UI,当然它会受到性能影响.

mod.getArticles = function( ){
    //mod.vars.ajaxCount could start at 0-2
    for( var i = mod.vars.ajaxCount; i < 3; i++ ){
        //mod.vars.pushIds is an array with the ids to be ajaxed in
        var id = mod.vars.pushIds[i];

        $.ajax({
            url: '/ajax/article/' + id + '/',
            type: "GET",
            dataType: 'HTML',
            error: function() {
                console.error('get article ajax error');
            }
        }).done( function( data ) {
            if (data.length) {
                mod.appendArticle( data );
            } else {
                console.error('get article ajax output error');
            }
        });
    }
};
Run Code Online (Sandbox Code Playgroud)

javascript queue ajax jquery promise

1
推荐指数
1
解决办法
1219
查看次数

标签 统计

ajax ×1

javascript ×1

jquery ×1

promise ×1

queue ×1