Created
April 21, 2016 13:42
-
-
Save ViniciusAugusto/71e1c28dcb7444b4ac7c7bcf69e655fa to your computer and use it in GitHub Desktop.
AngularJS - cancel all $timeouts on route change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment