Reputation: 1181
angular.forEach(response.jobs, function(job) {
fetchStatus(job);
});
var fetchStatus = function (job) {
$http
.get('http://monterey.dev/checkStatus/' + $scope.sessionId + '/' + job.jobId)
.success(function (response) {
job[job.jobId] = response;
if (response.status !== 'Completed' && response.status !== 'Aborted' && response.status !== 'Failed') {
$scope.dataTimeout = $timeout(function () {
fetchStatus(job)
}, 1000);
}
})
.error (function () {
$scope.dataTimeout = $timeout(function () {
fetchStatus(job)
}, 1000);
});
};
here is the destroy function, I want to cancel all the $scope.dataTimeout. At present it is canceling only $timeout.
$scope.$on("$destroy", function () {
$timeout.cancel($scope.dataTimeout);
});
Upvotes: 0
Views: 1787
Reputation: 171669
Since you are doing this in a loop, every iteration will over write $scope.dataTimeout
so it will only contain reference to the very last $timeout
You would need to create an array instead to be able to access them all.
var dataTimeout=[];
Then in the loop:
var timeOut = $timeout(function () {
fetchStatus(job);
}, 1000);
dataTimeout.push( timeOut );
And finally to cancel them all loop over the array and cancel each instance:
$scope.$on("$destroy", function () {
dataTimeout.forEach(function(timeout){
$timeout.cancel(timeout);
});
});
Upvotes: 4