[英]Trying to call a function inside a for loop in javascript
一秒钟之后,我就会发布代码。 我正在致电服务以拉回数据。 我将数据存储在一个数组中,每个数据都需要再次调用另一个服务。 所以我将代码设置如下:
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);
}
});
在删除promise和$ resolved之后,我开始for循环以调用其他函数。 initHelo函数只是为我建立一个列表,并使vars准备好进行下一次调用。 heloCall拨打我需要的其他电话。
这是helocall代码:
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();
});
};
在进行状态呼叫后,我会进行检查以确保它是有原因的状态,如果是,则进行我需要的呼叫。
我的问题是,这仅在需要时才有效。 我不确定我搞砸了。 我一直在努力使它整天正常工作。
它对我来说已经工作了很多次,但是我需要它一直在工作。 有一个更好的方法吗? 我实际上在同一文件中以相同的方式设置了另一个电话,每次都没有问题
任何建议将不胜感激
我想到了。 我需要运行检查以确保从服务返回的数组的长度不为0。这将导致问题,具体取决于首先返回的女巫调用。 我把这张支票放好之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.