search

Home  >  Q&A  >  body text

javascript - The for loop executes multiple asynchronous requests, but the order of the requested data is disrupted?

$http({
        'url':'/jtkj/yjmc',
        'method':'POST',
    })
    .success(function(res) {
        $scope.yjdh = res;
        console.log(res);
        var navUrl = ['/genSit','/news','/set','/manage','/cooperate','/admNet','/eplNet','/stuMan'];

        //传值,循环遍历出数据
        $scope.xlcd = [];
        for(var i=0;i<res.length;i++){
            $scope.yjid = res[i].yjid;
            console.log(res[i].yjid);
            ejnameGet($scope.yjid);
            $http({
                url:'/jtkj/ejmc',
                method:'POST',
                data:{yjid:$scope.yjid}
            }).success(function(xlcd1){
                //$scope.xlcd = xlcd1;
                console.log(xlcd1);
                xlcd.push(xlcd1);
            })
        }
        //console.log($scope.xlcd);
        $scope.xlcd = xlcd;
        console.log($scope.xlcd);
ringa_leeringa_lee2838 days ago639

reply all(3)I'll reply

  • 世界只因有你

    世界只因有你2017-06-12 09:28:54

    Push whichever asynchronous request is completed. It’s normal for chaos to occur; don’t use push, subscript directly through i, and just pay attention to the processing of the i value (IIFE can be used)

    reply
    0
  • 黄舟

    黄舟2017-06-12 09:28:54

    One idea is to construct a storage structure such as an array, and when the asynchronous function i is executed, put the data back into array[i].

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-12 09:28:54

    You can use the promise.all method to put the request into an array, so that the order will not be messed up.

    reply
    0
  • Cancelreply