试图在javascript中调用for循环中的函数

时间:2015-10-07 22:06:59

标签: javascript angularjs

我会在一秒钟内发布代码。我打电话给服务以取回数据。我获取数组中的数据,每个人都需要再次调用另一个服务。所以我设置了我的代码:

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();
    });
};

我删除了promise并解决了$我启动了我的for循环来调用我的其他函数。 initHelo函数只为我构建一个列表,并为下一次调用准备好vars。 heloCall拨打我需要的其他电话。

这是helocall代码:

    for (var i = 0; i < 10; i++) {
        setTimeout(function() {
            console.log(i);
        }, 0);
    }

我在进行状态调用后进行检查,以确保它是一个状态,它会有原因,如果我正在拨打我需要的电话。

我的问题是这只在它想要时才有用。我不知道我搞砸了什么。我一直试图让它一整天都正常工作。

它已经为我工作了很多次但我需要它一直工作。 有一个更好的方法吗? 我实际上在同一个文件中有另一个调用,它以相同的方式设置并且每次都可以正常工作

非常感谢任何建议

1 个答案:

答案 0 :(得分:0)

我明白了。我需要运行一个检查,以确保从服务返回的数组的长度不是0.这将导致问题取决于首先返回的女巫调用。我把这张支票放进去之后运行正常。