Skip to content

Instantly share code, notes, and snippets.

@ViniciusAugusto
Created April 21, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ViniciusAugusto/71e1c28dcb7444b4ac7c7bcf69e655fa to your computer and use it in GitHub Desktop.
Save ViniciusAugusto/71e1c28dcb7444b4ac7c7bcf69e655fa to your computer and use it in GitHub Desktop.
AngularJS - cancel all $timeouts on route change
//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