The use and removal of ANGULARJS timer "interval and timeout" _angularjs

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.