yavg
yavg

Reputation: 3051

How can I use the $timeout and $interval that was used in angular.js but for the Angular version?

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

Answers (2)

Pankaj Parkar
Pankaj Parkar

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

Padmapriya Vishnuvardhan
Padmapriya Vishnuvardhan

Reputation: 2166

Use setTimeout native function. There is no special services in angular to handle timers.

Upvotes: 0

Related Questions