How to cancel angularjs $ timeout when user goes from page

I use $timeout to regularly update information on one of the pages in my angularjs application. I would like to cancel $timeout when the user navigates from this page. Is there an easy way to do this?

+6
source share
1 answer

Ok, I found a solution after digging:

 $scope.$on('$destroy', function() { $timeout.cancel(timeout); }); 

Or for the new component syntax in Angular 1.5:

 this.$onDestroy = function() { if (timeout) { $timeout.cancel(timeout); } } 
+12
source

Source: https://habr.com/ru/post/948870/


All Articles