Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 403
Rasmus Avatar asked Feb 15 '13 15:02

Rasmus


People also ask

How do you reset a $timeout and disable a watch ()?

var customTimeout = $timeout(function () { // arbitrary code }, 55); $timeout. cancel(customTimeout); The same applies to “$interval()”. To disable a watch, just call it.

What does $Timeout do in AngularJS?

This '$timeout' service of AngularJS is functionally similar to the 'window. setTimeout' object of vanilla JavaScript. This service allows the developer to set some time delay before the execution of the function.

What is the function of the $timeout service?

The $timeout service can be used to call another JavaScript function after a given time delay. The $timeout service only schedules a single call to the function. For repeated calling of a function, see $interval later in this text.

What does timeout flush do?

Flushes the queue of pending tasks. This method is essentially an alias of $flushPendingTasks .


1 Answers

Use $timeout.cancel like this:

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

Reference

like image 96
EpokK Avatar answered Sep 22 '22 09:09

EpokK