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.
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.
A more performant approach :
var intervals = [];
intervals.push($interval(function() { /*doStuff*/ }, /*timeout*/));
intervals.push($interval(function() { /*doDifferentStuff*/ }, /*timeout*/));
//doing some stuff
while(intervals.length){
$interval.cancel(intervals.pop());
}
© 2022 - 2024 — McMap. All rights reserved.