简体   繁体   中英

Trying to call a function inside a for loop in javascript

Ill post the code in a second. I am making a call to a service to pull data back. I get the data in an array and each one needs to make another call to another service. so I set my code up like so:

Services.getHelo({
    assetSurfaceId: $scope.assetSurfaceId
}).then(function (resp) {
    delete resp["$promise"];
    delete resp["$resolved"];

    $scope.entity.helo = resp;

    for (var i = 0; i < $scope.entity.helo.length; i++) {
        heloCall($scope.entity.helo, i);
        initHelo($scope.entity.helo, i);
    }
});

After I delete the promise and $resolved I start my for loop to call to my other functions. the initHelo function just builds a list for me and gets the vars ready for the next call into it. the heloCall makes the other calls i need.

Here is the helocall code:

var heloCall = function (r, i) {
    jQuery("#helo").mask("Loading Surface Asset Helos...");
    Services.getStatus({
        toEntityId: r[i].assetHeloId,
        toEntityTypeId: widget_consts.ASSET_HELICOPTER
    }).then(function (s) {
        delete s["$promise"];
        delete s["$resolved"];
        $scope.entity.helo[i].status = [];
        $scope.entity.helo[i].status = s;
        if (s[0].statusLkupShortDesc === "PMC" || s[0].statusLkupShortDesc === "NMC") {
            Services.getReason({
                toEntityId: s[0].statusId
            }).then(function (reason) {
                delete reason["$promise"];
                delete reason["$resolved"];
                if (reason) {
                    $scope.entity.helo[i].status.reason = [];
                    $scope.entity.helo[i].status.reason = reason;
                    initHelo($scope.entity.helo, i);

                }
            });
            Services.getComment({
                toEntityId: r[i].assetHeloId
            }).then(function (info) {
                delete info["$promise"];
                delete info["$resolved"];
                if (info) {
                    $scope.entity.helo[i].status.remark = {};
                    $scope.entity.helo[i].status.remark = info;
                    initHelo($scope.entity.helo, i);
                }
            });
        }
        initHelo($scope.entity.helo, i);
        jQuery("#helo").unmask();
    });
};

I do a check after the status call is made to make sure it is a status that Would have reasons with it and if it is i make the calls i need.

My issue is this only works when it wants to. I am not sure what i screwed up. I have been trying to get it to work right all day.

It has worked many times for me but I need it to work all the time. Is there a better way to do this? I actually have another call in this same file that is set up the same way and works each time with no issues

Any advice would be greatly appreciated

I figured it out. I needed to run a check to make sure that the length of the array returned from the service was not 0. it would cause issues depending on witch call was returned first. after I put this check it in runs fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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