Michael
Michael

Reputation: 3739

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?

Upvotes: 7

Views: 3305

Answers (1)

Michael
Michael

Reputation: 3739

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);
    }
}

Upvotes: 13

Related Questions