javascript - The for loop executes multiple asynchronous requests, but the order of the requested data is disrupted?
ringa_lee
ringa_lee 2017-06-12 09:26:54
0
3
633
$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_lee
ringa_lee

ringa_lee

reply all(3)
世界只因有你

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)

黄舟

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].

大家讲道理

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template