This paper illustrates the use and removal of ANGULARJS timer. Share to everyone for your reference, specific as follows:
1. Compared to JS in Settimeinterval and settimeout,angularjs through the interval to achieve the effect of the timer, through the timeout to achieve time delay.
$timeout//Implementation is deferred execution
$interval//implementation is the effect of the timer
We look at these two services separately.
(1) Timeout
Timeout is equivalent to the delay in the implementation of JS native, the function of the service returned is a Promise object.
var timer= $timeout (function () {
console.log (' Hello World ')
},2000); The function delays 2 seconds executing
timer.then (function () {Console.log (' Create Success ')}, function
() {console.log (' Create unsuccessful ')};
(2) Interval
Interval is similar to the timeout service, and the creation timer returns a Promise object.
var timer= $interval (function () {
console.log (' Hello World ')
},2000); Interval 2 seconds to perform
timer.then (function () {Console.log (' Create Success ')},
function () {Console.log (' Create unsuccessful ')};
2. How to remove the timer
In Angularjso, especially when paging or routing, we need to remove the timer for the response, and we can listen to the routing switch time by using the on method. When the DOM structure is changed, the on method is executed:
$scope. $on (' Destroy ', function () {
$interval. Cancel ($scope. timer);
})//In Controller, add $on function
More readers interested in ANGULARJS related content can view the site topics: "Angularjs Introduction and Advanced Tutorials" and "ANGULARJS MVC Framework Summary"
I hope this article will help you to Angularjs program design.