我需要进行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)