AngularJS停止定时器$interval

时间:2021-04-24 23:24:07

1、问题背景

     设置一个定时器,给定两个变量startData和endData,计时开始后分别减去5和50;单击停止暂停定时器,重置后数据恢复到原来的数据。


2、实现源码

[html]  view plain  copy  print ?
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.         <title>AngularJS停止定时器</title>  
  6.         <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>  
  7.         <script>  
  8.             var app = angular.module("intervalApp",[]);  
  9.             app.controller("intervalController",["$scope","$interval",function($scope,$interval){  
  10.                 $scope.startData = 100;  
  11.                 $scope.endData = 1000;  
  12.                   
  13.                 var stopEvent;  
  14.                   
  15.                 $scope.startEvent = function(){  
  16.                     if(angular.isDefined(stopEvent)) return;  
  17.                       
  18.                     stopEvent = $interval(function(){  
  19.                         if($scope.startData > 50 && $scope.endData > 500)  
  20.                         {  
  21.                             $scope.startData = $scope.startData - 5;  
  22.                             $scope.endData = $scope.endData - 50;  
  23.                         }  
  24.                         else  
  25.                         {  
  26.                             $scope.stopClick();  
  27.                         }  
  28.                     },1000)  
  29.                 };  
  30.                   
  31.                 $scope.stopClick = function(){  
  32.                     if(angular.isDefined(stopEvent))  
  33.                     {  
  34.                         $interval.cancel(stopEvent);  
  35.                         stopEvent = undefined;  
  36.                     }  
  37.                 };  
  38.                   
  39.                 $scope.resetEvent = function(){  
  40.                     $scope.startData = 100;  
  41.                     $scope.endData = 1000;  
  42.                 };  
  43.                   
  44.                 $scope.$on("$destory",function(){  
  45.                     $scope.stopClick();  
  46.                 });  
  47.             }]);  
  48.         </script>  
  49.     </head>  
  50.     <body ng-app="intervalApp">  
  51.         <div ng-controller="intervalController">  
  52.             <button data-ng-click="startEvent()">开始</button>  
  53.             <button data-ng-click="stopClick()">停止</button>  
  54.             <button data-ng-click="resetEvent()">重置</button><br>  
  55.             <div>开始数据:{{startData}}</div><br>  
  56.             <div>结束数据:{{endData}}</div><br>  
  57.         </div>  
  58.     </body>  
  59. </html>  

3、实现结果

(1)初始化

    AngularJS停止定时器$interval

(2)点击开始

   AngularJS停止定时器$interval

(3)点击停止

    AngularJS停止定时器$interval

(4)点击重置

     AngularJS停止定时器$interval


转自:http://blog.csdn.net/you23hai45/article/details/52771974