javascript - how can i use $interval in for loop -
hi want run $interval in loop
what did
- i implemented below code auto refreshing
- i am] checking data array status queued object
- then calling refresh instance (it ajax call status of instance)
- but suppose if there more 1 instance having queued status refreshing instnace last one
what wnat want refresh isntnaces
ex:
data=[{id:1,status:'queued'},{id:2,status:'active'},{id:3,status:'queued'},{id:4,status:'active'},{id:5,status:'queued'}]; then id:5 instnace refreshing want instaces 1,3,5
for (var = 0; < data.length; i++) { if(data[i].status =='queued') { var id = data[i].id; promiseobj[id] = $interval( function(){ $scope.refreshinstance('active',id); }, 20000); } }
the problem here use of closure inside loop.
but in case can fix using easier foreach() loop
angular.foreach(data, function (item, idx) { if (item.status == 'queued') { promiseobj[item.id] = $interval(function () { $scope.refreshinstance('active', item.id); }, 20000); } });
Comments
Post a Comment