Hi i want to remove all running $interval
in Angular. in my page there are many $interval and on button click i want to remove all interval.How will i do it .
Any help is appreciated.
According to the documentation $interval
returns a promise, and the interval can be cancelled using $interval.cancel(promise)
.
I can't see any methods for cancelling all intervals, but if you keep the returned promises in an array you can iterate over that to cancel them all.
var intervals = []
intervals.push($interval(function() { /*doStuff*/ }, /*timeout*/));
intervals.push($interval(function() { /*doDifferentStuff*/ }, /*timeout*/));
...
angular.forEach(intervals, function(interval) {
$interval.cancel(interval);
});
intervals.length = 0; //clear the array
If your intervals are spread over different controllers, use a service to keep track of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With