AngularJs $interval and $timeout

Source: Internet
Author: User
Tags time in milliseconds

$interval

Window.setinterval's angular packaging form. FN is a function that is executed after each delay time.

The return value of the interval function is a promise. This commitment is notified at each interval scale and is canceled after the specified number of iterations, and unrestricted execution if the number of iterations is undefined. The value of the notification will be the number of iterations to run. Cancels an interval, calling $intreval.cancel (Promise).

Note: You should destroy the service after you have completed it. Especially when a controller or directive element is destroyed, $interval is not destroyed. You should consider canceling the interval event at the appropriate time.

Use : $interval (Fn,delay,[count],[invokeapply],[pass]);

fn: A function that will be executed repeatedly.

Delay: The number of milliseconds between each call.

Count: Number of cycles, or infinite loop if not set.

Invokeapply: If set to false, the dirty value check is avoided, otherwise $apply will be called.

Pass: Additional parameters for the function.

Method :

Cancel (promise);

Cancels the task associated with the commitment.

Promise: The return value of the $interval function.

Using code:

  (function () {    angular.module ("Demo", [])    . Controller ("Testctrl", ["$interval", Testctrl]);    functionfunction () {Console.log ("Hello World"); $interval (toDo, +);};} ());

$timeout

Window.settimeout's angular packaging form. The FN function is packaged into a try/catch block that represents any exception in the $exceptionhandler service.

The return value of the timeout function is a promise, and when the time-out is reached, the commitment is resolved and the timeout function is executed.

Need to cancel timeout, call $timeout.cancel (Promise);

Use : $timeout (fn,[delay],[invokeapply]);

fn: A function that will be delayed execution.

Delay: Time in milliseconds.

Invokeapply: If set to false, the dirty value detection is skipped, otherwise $apply will be called.

Method :

Cancel (promise);

Cancels the task associated with the commitment. The result is that the commitment will be resolved in a way that is abandoned.

Promise: $timeout The promise returned by the function.

  (function () {    angular.module ("Demo", [])    . Controller ("Testctrl", ["$timeout", Testctrl]);    functionfunction () {Console.log ("Hello World");}; $timeout (todo,5000)};} ());

$interval, $timeout How to use it:

1. First inject $interval, $timeout, as parameters into the controller, such as Rds.controller (' Controllerctrl ', [' app ', ' $scope ', ' $http ', ' $ Routeparams ', ' $filter ', ' $location ', ' $interval ', ' $timeout ',

    function (app, $scope, $http, $routeParams, $filter, $location, $interval, $timeout) {
2. Write the method, such as $scope, where needed. timer = $interval (function () {
    $scope. Backup ("1");
}, 10000);
At first thought is to call the method directly in the function () {} location, found that the method went to the timer directly executed the method and did not loop execution, and later found to need to call the method written in function, 10000 means 10 seconds to cycle. If you want to define the number of loops Just write the following $scope. Timer = $interval (function () {
    $scope. Backup ("1");
}, 10000,3); 3 means the loop is automatically stopped 3 times.
3. Terminate Timer $interval. Cancel ($scope. timer);
4. The usage of the $timeout is similar, except that $timeout is cycled once after a delay of a number of seconds, and $interval is cycled every second, if not terminated.
The following is an excerpt from the blog on the relatively tall introduction

Interval

Window.setinterval's angular packaging form. FN is a function that is executed after each delay time.

The return value of the interval function is a promise. This commitment is notified at each interval scale and is canceled after the specified number of iterations, and unrestricted execution if the number of iterations is undefined. The value of the notification will be the number of iterations to run. Cancels an interval, calling $intreval.cancel (Promise).

Note: You should destroy the service after you have completed it. Especially when a controller or directive element is destroyed, $interval is not destroyed. You should consider canceling the interval event at the appropriate time.

Use : $interval (Fn,delay,[count],[invokeapply],[pass]);

fn: A function that will be executed repeatedly.

Delay: The number of milliseconds between each call.

Count: Number of cycles, or infinite loop if not set.

Invokeapply: If set to false, the dirty value check is avoided, otherwise $apply will be called.

Pass: Additional parameters for the function.

Method :

Cancel (promise);

Cancels the task associated with the commitment.

Promise: The return value of the $interval function.

Using code:

  (function () {    angular.module ("Demo", [])    . Controller ("Testctrl", ["$interval", Testctrl]);    function Testctrl ($interval) {      var toDo = function () {          console.log ("Hello World");      };      $interval (ToDo, up, ten);    };  } ());

$timeout

Window.settimeout's angular packaging form. The FN function is packaged into a try/catch block that represents any exception in the $exceptionhandler service.

The return value of the timeout function is a promise, and when the time-out is reached, the commitment is resolved and the timeout function is executed.

Need to cancel timeout, call $timeout.cancel (Promise);

Use : $timeout (fn,[delay],[invokeapply]);

fn: A function that will be delayed execution.

Delay: Time in milliseconds.

Invokeapply: If set to false, the dirty value detection is skipped, otherwise $apply will be called.

Method :

Cancel (promise);

Cancels the task associated with the commitment. The result is that the commitment will be resolved in a way that is abandoned.

Promise: $timeout The promise returned by the function.

  (function () {    angular.module ("Demo", [])    . Controller ("Testctrl", ["$timeout", Testctrl]);    function Testctrl ($timeout) {      var toDo = function () {          console.log ("Hello World");      };      $timeout (todo,5000)    };  } ());

The approximate use of the method can be used with native JS SetInterval and settimeout, some of the use of tips can be used in the browser single-threaded event execution aspects ... Read more about the wolf's article "JavaScript single-threaded and browser event loop brief", you can also focus on its public number ["Shuang_lang_shuo"], there are many good articles.


AngularJs $interval and $timeout

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.