神刀安全网

Understanding $timeout Service In AngularJS

Introduction

The window.setTimeout in JavaScript method executes function or evaluates an expression after a specified number of milliseconds. This function is only executed one time. We need to use setInterval method, if we required executing this function repetitively. It takes three parameters: JavaScript function need to execute, number of milliseconds to wait before executing supplied function and additional parameter (optional). The $timeout service is wrapper around this method. Internally function is wrapped into a try/catch block and if any exception is occurred during the execution, $exceptionHandler service will handle it.

Syntax

$timeout([function], [delay time], [invokeApply], [Pass]);   

Arguments

  • function: function which execution would be delayed.
  • delay time: delay time in milliseconds. The default value of this parameter is 0.
  • invokeApply: AngularJS will invoke the function within $apply block and checking model dirty if it is set to true, else it skips model dirty checking.
  • pass: pass here additional parameters required to execute function.

This service return promise and it will be resolved when delay time has passed.

Example

HTML & Controller

<!DOCTYPEhtml>   <html>      <head>       <title>$timeout Service in AngularJS</title>       <scriptsrc="angular.js">           </script>   </head>   <bodyng-app="app">       <h4>$timeout Example</h4>       <divng-controller="HelloController">           <p>After 5 seconds alert box will appear.</p>           </div>           <script>               var app = angular.module("app", []);               app.controller("HelloController", function($scope, $timeout)               {                   $timeout(showMessage, 5000);                   functionshowMessage()                    {                       alert('Hi, Jignesh!');                   }               });           </script>           </body>      </html>   

Output:

Understanding $timeout Service In AngularJS

$timeout.cancel()

This method cancels a task related with the promise. It will be resolved with rejection. This method returns true if task is not executed yet and the task canceled successfully.

Example

HTML and Controller

<!DOCTYPEhtml>   <html>      <head>       <title>$timeout Service in AngularJS</title>       <scriptsrc="angular.js">           </script>   </head>   <bodyng-app="app">       <h4>$timeout Example</h4>       <divng-controller="HelloController">           <p>After 10 seconds alert box will appear.</p>           <buttonng-click="cancelTimeout()">Cancel Timeout</button>               </div>               <script>                   var app = angular.module("app", []);                   app.controller("HelloController", function($scope, $timeout)                       {                       var timer = $timeout(showMessage, 10000);                       functionshowMessage()                        {                           alert('Hi, Jignesh!');                       }                       $scope.cancelTimeout = function()                        {                           $timeout.cancel(timer);                       }                   });               </script>               </body>      </html>   

Output

When user click on “Cancel Timeout” button before 10 sec, AngularJS will cancel the task associated with the promise.

The $timeout function return a promise and we can bind it to resolved or reject events. We can cancel the underlying timer by passing promise off to the cancel method of $timeout service. It is very important to cancel timer because in AngularJS, timer is executing even if it is no longer relevant to the state of the application. This will happen silently but it might cause unexpected behavior of the Angular application. It is recommended to call the $timeout.cancel method containing Directive or Controller receives the $destroy event.

Summary

There are many cases where we need some small time delay and in JavaScript, this can be achieved using setTimeout() function. AngularJS provides wrapper service named $timeout. When we call setTimeout function from AngularJS, $scope.apply function need to be called to ensure that all the changes to the scope will be reflected. When we use $timeout service, we need not call $apply function.

Hope this will help you.

原文  http://www.c-sharpcorner.com/UploadFile/ff2f08/understanding-timeout-service-in-angularjs169/

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Understanding $timeout Service In AngularJS

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮