简体   繁体   English

在for循环中进行多个ajax调用

[英]making multiple ajax calls within a for loop

I'm a relative newbie to javascript and I'm trying to make multiple ajax calls within a for loop.我是 javascript 的相对新手,我正在尝试在 for 循环中进行多个 ajax 调用。 It loops through the elements of an array using a different url for an ajax call each time it goes through the loop.每次遍历循环时,它都会使用不同的 url 为 ajax 调用循环遍历数组的元素。 The problem is that the value of the variable 'test' is always equal to "condition4".问题是变量“test”的值总是等于“condition4”。 I'm used to other languages where the value of 'test' would be "condition1", then "condition2" etc as it goes through the for loop.我已经习惯了其他语言,其中 'test' 的值是“condition1”,然后是“condition2”等,因为它通过 for 循环。 Here is a simplified version of my code:这是我的代码的简化版本:

var myData = [];
var cnt = 0;
var link;
var myCounter = 0;
var myArray = ["condition1", "condition2", "condition3", "condition4"];

for (x = 0; x < myArray.length; x++) {
    link = "https://test.com/" + myArray[x];
    myCounter = x;
    GetJSON(function (results) {
        for (i = 0; i < results.data.length; i++) {
            var id = results.data[i].identifier;
            var test = myArray[myCounter];
            myData[cnt] = { "id": id, "test": test };
            cnt++;
        }
    });
}

function GetJSON(callback) {
    $.ajax({
        url: link,
        type: 'GET',
        dataType: 'json',
        success: function (results) {
            callback(results);
        }
    });
}

I think you can solve this issue by sending and receiving myCounter value to server我认为您可以通过向服务器发送和接收 myCounter 值来解决此问题

 for (x = 0; x < myArray.length; x++) { link = "https://test.com/" + myArray[x]; myCounter = x; $.ajax({ url: link, type: 'GET', dataType: 'json', data: { myCounter: myCounter} success: function(results) { for (i = 0; i < results.data.length; i++) { var id = results.data[i].identifier; var test = results.data[i].myCounter myData[cnt] = { "id": id, "test": test }; cnt++; } } }); }

When you are executing the loop, it attaches the myCounter reference.当您执行循环时,它会附加myCounter引用。 Then, due to the async task, when it finishes and call 'myCounter', it has already achieved the number 4. So, when it call 'myCounter', it is 4. To isolate the scope, you need to create a new scope every iteration and isolating each value of 'myCounter'然后,由于异步任务,当它完成并调用'myCounter'时,它已经达到了数字4。所以,当它调用'myCounter'时,它是4。为了隔离作用域,您需要创建一个新作用域每次迭代并隔离“myCounter”的每个值

 for (x = 0; x < myArray.length; x++) { link = "https://test.com/" + myArray[x]; myCounter = x; //IIFE (function() { var ownCounter = myCounter; //Isolating counter GetJSON(function (results) { for (i = 0; i < results.data.length; i++) { var id = results.data[i].identifier; var test = myArray[ownCounter]; myData[cnt] = { "id": id, "test": test }; cnt++; } }); })(); }

Or...或者...

 for (let x = 0; x < myArray.length; x++) { link = "https://test.com/" + myArray[x]; myCounter = x; GetJSON(function (results) { for (i = 0; i < results.data.length; i++) { var id = results.data[i].identifier; var test = myArray[x]; myData[cnt] = { "id": id, "test": test }; cnt++; } }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM