EVOLUTION-MANAGER
Edit File: $interval.html
<a href='https://github.com/angular/angular.js/edit/v1.3.x/src/ng/interval.js?message=docs($interval)%3A%20describe%20your%20change...#L10' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit"> </i>Improve this Doc</a> <a href='https://github.com/angular/angular.js/tree/v1.3.9/src/ng/interval.js#L10' class='view-source pull-right btn btn-primary'> <i class="glyphicon glyphicon-zoom-in"> </i>View Source </a> <header class="api-profile-header"> <h1 class="api-profile-header-heading">$interval</h1> <ol class="api-profile-header-structure naked-list step-list"> <li> - service in module <a href="api/ng">ng</a> </li> </ol> </header> <div class="api-profile-description"> <p>Angular's wrapper for <code>window.setInterval</code>. The <code>fn</code> function is executed every <code>delay</code> milliseconds.</p> <p>The return value of registering an interval function is a promise. This promise will be notified upon each tick of the interval, and will be resolved after <code>count</code> iterations, or run indefinitely if <code>count</code> is not defined. The value of the notification will be the number of iterations that have run. To cancel an interval, call <code>$interval.cancel(promise)</code>.</p> <p>In tests you can use <a href="api/ngMock/service/$interval#flush"><code>$interval.flush(millis)</code></a> to move forward by <code>millis</code> milliseconds and trigger any functions scheduled to run in that time.</p> <div class="alert alert-warning"> <strong>Note</strong>: Intervals created by this service must be explicitly destroyed when you are finished with them. In particular they are not automatically destroyed when a controller's scope or a directive's element are destroyed. You should take this into consideration and make sure to always cancel the interval at the appropriate moment. See the example below for more details on how and when to do this. </div> </div> <div> <h2 id="usage">Usage</h2> <p><code>$interval(fn, delay, [count], [invokeApply]);</code></p> <section class="api-section"> <h3>Arguments</h3> <table class="variables-matrix input-arguments"> <thead> <tr> <th>Param</th> <th>Type</th> <th>Details</th> </tr> </thead> <tbody> <tr> <td> fn </td> <td> <a href="" class="label type-hint type-hint-function">function()</a> </td> <td> <p>A function that should be called repeatedly.</p> </td> </tr> <tr> <td> delay </td> <td> <a href="" class="label type-hint type-hint-number">number</a> </td> <td> <p>Number of milliseconds between each function call.</p> </td> </tr> <tr> <td> count <div><em>(optional)</em></div> </td> <td> <a href="" class="label type-hint type-hint-number">number</a> </td> <td> <p>Number of times to repeat. If not set, or 0, will repeat indefinitely.</p> <p><em>(default: 0)</em></p> </td> </tr> <tr> <td> invokeApply <div><em>(optional)</em></div> </td> <td> <a href="" class="label type-hint type-hint-boolean">boolean</a> </td> <td> <p>If set to <code>false</code> skips model dirty checking, otherwise will invoke <code>fn</code> within the <a href="api/ng/type/$rootScope.Scope#$apply">$apply</a> block.</p> <p><em>(default: true)</em></p> </td> </tr> </tbody> </table> </section> <h3>Returns</h3> <table class="variables-matrix return-arguments"> <tr> <td><a href="" class="label type-hint type-hint-promise">promise</a></td> <td><p>A promise which will be notified on each iteration.</p> </td> </tr> </table> <h2>Methods</h2> <ul class="methods"> <li id="cancel"> <h3><p><code>cancel(promise);</code></p> </h3> <div><p>Cancels a task associated with the <code>promise</code>.</p> </div> <h4>Parameters</h4> <table class="variables-matrix input-arguments"> <thead> <tr> <th>Param</th> <th>Type</th> <th>Details</th> </tr> </thead> <tbody> <tr> <td> promise </td> <td> <a href="" class="label type-hint type-hint-promise">promise</a> </td> <td> <p>returned by the <code>$interval</code> function.</p> </td> </tr> </tbody> </table> <h4>Returns</h4> <table class="variables-matrix return-arguments"> <tr> <td><a href="" class="label type-hint type-hint-boolean">boolean</a></td> <td><p>Returns <code>true</code> if the task was successfully canceled.</p> </td> </tr> </table> </li> </ul> <h2 id="example">Example</h2><p> <div> <a ng-click="openPlunkr('examples/example-example108')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-example108" module="intervalExample"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><script> angular.module('intervalExample', []) .controller('ExampleController', ['$scope', '$interval', function($scope, $interval) { $scope.format = 'M/d/yy h:mm:ss a'; $scope.blood_1 = 100; $scope.blood_2 = 120; var stop; $scope.fight = function() { // Don't start a new fight if we are already fighting if ( angular.isDefined(stop) ) return; stop = $interval(function() { if ($scope.blood_1 > 0 && $scope.blood_2 > 0) { $scope.blood_1 = $scope.blood_1 - 3; $scope.blood_2 = $scope.blood_2 - 4; } else { $scope.stopFight(); } }, 100); }; $scope.stopFight = function() { if (angular.isDefined(stop)) { $interval.cancel(stop); stop = undefined; } }; $scope.resetFight = function() { $scope.blood_1 = 100; $scope.blood_2 = 120; }; $scope.$on('$destroy', function() { // Make sure that the interval is destroyed too $scope.stopFight(); }); }]) // Register the 'myCurrentTime' directive factory method. // We inject $interval and dateFilter service since the factory method is DI. .directive('myCurrentTime', ['$interval', 'dateFilter', function($interval, dateFilter) { // return the directive link function. (compile function not needed) return function(scope, element, attrs) { var format, // date format stopTime; // so that we can cancel the time updates // used to update the UI function updateTime() { element.text(dateFilter(new Date(), format)); } // watch the expression, and update the UI on change. scope.$watch(attrs.myCurrentTime, function(value) { format = value; updateTime(); }); stopTime = $interval(updateTime, 1000); // listen on DOM destroy (removal) event, and cancel the next UI update // to prevent updating time after the DOM element was removed. element.on('$destroy', function() { $interval.cancel(stopTime); }); } }]); </script> <div> <div ng-controller="ExampleController"> Date format: <input ng-model="format"> <hr/> Current time is: <span my-current-time="format"></span> <hr/> Blood 1 : <font color='red'>{{blood_1}}</font> Blood 2 : <font color='red'>{{blood_2}}</font> <button type="button" data-ng-click="fight()">Fight</button> <button type="button" data-ng-click="stopFight()">StopFight</button> <button type="button" data-ng-click="resetFight()">resetFight</button> </div> </div></code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-example108/index.html" name="example-example108"></iframe> </div> </div> </p> </div>