Reputation: 3051
In Angular.js I used $timeout
and $interval
(similar to setInterval
and setTimeout
in javascript).
$timeout(function(){})
$interval(function(){},5000)
to cancel the interval I used
$interval.cancel($scope.interval);
and when leaving the view and avoiding that the $interval
was still running I used. I am new at Angular.
$scope.$on('$ destroy', function () {
$interval.cancel($scope.interval)
})
How can I use the equivalent of these 2 functions that I used in angular.js to angular?
Upvotes: 0
Views: 252
Reputation: 136144
Use setInterval
& setTimeout
as is inside Angular, but use ngOnDestroy
lifecycle hook to clear the timers stuff before component instance is diminished. This lifecycle hook helps to clearing the stuff, it is as similar as that of angularjs $destroy
event.
ngOnDestroy(){
//do clear intervals and timeouts here
//clearInterval(yourInterval);
//clearTimeout(yourTimeout);
}
Upvotes: 1
Reputation: 2166
Use setTimeout native function. There is no special services in angular to handle timers.
Upvotes: 0