Rasmus
Rasmus

Reputation: 4180

Cancel a AngularJS $timeout on routeChange

On a specific page in my application I think of doing a server-call to update information on a set interval. I stumbled upon a problem though. I want to cancel my $timeout when a user navigates away from the page in question so that the application doesn't try to work with stuff that isn't there anymore.

Any ideas on how to work around this?

Upvotes: 37

Views: 32362

Answers (1)

EpokK
EpokK

Reputation: 38092

Use $timeout.cancel like this:

yourTimer = $timeout(function() { /* ... */ }, 5000);
$timeout.cancel(yourTimer);

Reference

Upvotes: 107

Related Questions