Order pending jquery ajax requests

I have a list of languages, ['en', 'de']etc., and I want to take a complex ajax request for each language. On the first ajax request, I get the percentage for each language.

var languages = ['en', 'de', 'es', 'fr', 'it', 'cn'];
intents_dic = [];
chart_quantity = ['quantity'];
for (var x = 0; x < languages.length; x++){
    $.ajax({
    url: 'url',
    type: 'GET',
    async: false,
    success: function(data){
        for (i in data){
            intents_dic.push(data[i]['id'])
        }
        var intents_count = 0;
        var deferreds = [];

And in the second stage, I need to calculate the number of phrases for each intention from the previous call. I am using a pending list for this task.

for (var id = 0; id < intents_dic.length; id++){
    deferreds.push($.ajax({
    url: 'url',
    type: 'GET',
    success: function(data){
        intents_count += data['templates'].length;
        }
    }))
}
$.when.apply(null, deferreds).done(function() {
    chart_quantity.push(intents_count);
})

But when pending ajax requests are completed, the order of completion is different from the order of the list of languages ​​(this is a schematic diagram of the diagram). Can this problem be fixed, or is it ajax call function? Thank.

+4
source share
1 answer

, , jsfiddle ( ).

for(var i = 0; i < 5; i++){
    setTimeout(function(x){
        done(x,results);
    }(i),1000);
}

http://jsfiddle.net/htuz3t3m/2/

0

All Articles