Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel angularjs $timeout when user navigate away from the page

I use $timeout to routinely update the info on one of the page in my angularjs app. I'd like to cancel the $timeout when user navigate away from this page. Is there a simple way to do that?

like image 909
Michael Avatar asked Jul 07 '13 01:07

Michael


1 Answers

Ok, I found the solution after digging around:

$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);
    }
}
like image 152
Michael Avatar answered Oct 14 '22 23:10

Michael