• AngularJS停止定时器


    1、问题背景

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


    2、实现源码

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>AngularJS停止定时器</title>
    		<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    		<script>
    			var app = angular.module("intervalApp",[]);
    			app.controller("intervalController",["$scope","$interval",function($scope,$interval){
    				$scope.startData = 100;
    				$scope.endData = 1000;
    				
    				var stopEvent;
    				
    				$scope.startEvent = function(){
    					if(angular.isDefined(stopEvent)) return;
    					
    					stopEvent = $interval(function(){
    						if($scope.startData > 50 && $scope.endData > 500)
    						{
    							$scope.startData = $scope.startData - 5;
    							$scope.endData = $scope.endData - 50;
    						}
    						else
    						{
    							$scope.stopClick();
    						}
    					},1000)
    				};
    				
    				$scope.stopClick = function(){
    					if(angular.isDefined(stopEvent))
    					{
    						$interval.cancel(stopEvent);
    						stopEvent = undefined;
    					}
    				};
    				
    				$scope.resetEvent = function(){
    					$scope.startData = 100;
    					$scope.endData = 1000;
    				};
    				
    				$scope.$on("$destory",function(){
    					$scope.stopClick();
    				});
    			}]);
    		</script>
    	</head>
    	<body ng-app="intervalApp">
    		<div ng-controller="intervalController">
    			<button data-ng-click="startEvent()">开始</button>
    			<button data-ng-click="stopClick()">停止</button>
    			<button data-ng-click="resetEvent()">重置</button><br>
    			<div>开始数据:{{startData}}</div><br>
    			<div>结束数据:{{endData}}</div><br>
    		</div>
    	</body>
    </html>
    

    3、实现结果

    (1)初始化

        

    (2)点击开始

       

    (3)点击停止

        

    (4)点击重置

         


  • 相关阅读:
    css切图Sprites
    javascript改变position值实现菜单滚动至顶部后固定
    Cannot create type class java.io.File from value
    关于如何拍摄瓷器(转)
    Struts2的jar问题
    vim的基本操作
    Flask基础
    Flask入门
    MongoDB 之 数据类型
    基于DBUtils实现数据库连接池
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13314047.html
Copyright © 2020-2023  润新知