angularjs 定时器 销毁

angular.module(‘app‘, [])

  .controller(‘ItemController‘, function($scope, $interval) {

    // store the interval promise in this variable
    var promise;

    // simulated items array
    $scope.items = [];

    // starts the interval
    $scope.start = function() {
      // stops any running interval to avoid two intervals running at the same time
      $scope.stop(); 

      // store the interval promise
      promise = $interval(setRandomizedCollection, 1000);
    };

    // stops the interval
    $scope.stop = function() {
      $interval.cancel(promise);
    };

    // starting the interval by default
    $scope.start();

    // stops the interval when the scope is destroyed,
    // this usually happens when a route is changed and
    // the ItemsController $scope gets destroyed. The
    // destruction of the ItemsController scope does not
    // guarantee the stopping of any intervals, you must
    // be responsible for stopping it when the scope is
    // is destroyed.
    $scope.$on(‘$destroy‘, function() {
      $scope.stop();
    });

    function setRandomizedCollection() {
      // items to randomize 1 - 11
      var randomItems = parseInt(Math.random() * 10 + 1); 

      // empties the items array
      $scope.items.length = 0; 

      // loop through random N times
      while(randomItems--) {

        // push random number from 1 - 10000 to $scope.items
        $scope.items.push(parseInt(Math.random() * 10000 + 1));
      }
    }

  });
时间: 2024-08-10 15:10:15

angularjs 定时器 销毁的相关文章

vue组件里定时器销毁问题

解决方案1: data() { return { timer: null // 定时器名称 } }, this.timer = (() => { // 某些操作 }, 1000) 最后在beforeDestroy()生命周期内清除定时器: beforeDestroy() { clearInterval(this.timer); this.timer = null; } 方案1有两点不好的地方: 它需要在这个组件实例中保存这个 timer,如果可以的话最好只有生命周期钩子可以访问到它.这并不算严重

AngularJS - 定时器 倒计时例子

<body> <div ng-app="myApp"> <div ng-controller="firstController"> <input type="button" ng-value ="text" ng-disabled="isDisable"/> <input type="text" value="{{text}

IOS开发--动画篇--&gt;计时定时器

计时定时器 设计计时定时器的步骤: 1,创建可变数组,存放 0~100 的数字 2,用for循环创建 0~100 的数字,并将其放入可变数组中 3,创建定时器:创建定时器: 参数1:设置时间间隔:参数2:目标:参数3:定时器绑定的方法:参数4:补充信息,一般为空:参数5:是否重复 _timer = [NSTimerscheduledTimerWithTimeInterval:0.1target:self selector:@selector(outPut) userInfo:nilrepeats

添加定时器

在iOS中有很多地方需要用到定时器的,例如我们经常看到的图片轮播器,当你打开页面的时候它是自动进行轮播,这便是用到了定时器,那么定时器又是如何添加的呢?下面就给大家用一小段代码来演示一下: 首先要添加一个定时器: - (void)addTimer{ //添加一个定时器 [NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; //提

MFC Timer定时器

知识点: 定时器Timer 创建定时器 销毁定时器 代码测试 一. 创建定时器 UINT SetTimer( HWND hWnd, // 指定关联定时器的窗口句柄,在MFC版将省略此参数 UINT nIDEvent, // 定时器ID UINT uElapse, // 时间间隔 单位毫秒 TIMERPROC lpTimerFunc //定时器回调函数地址 ); 定时器回调函数格式 VOID CALLBACK TimerProc( HWND hwnd, // handle of window fo

Windows定时器

目录 第1章定时器    1 1.1 创建定时器    1 1.2 销毁定时器    1 1.3 定时器的运作    1 1.3.1 产生WM_TIMER消息    1 1.3.2 分发WM_TIMER消息    2 1.4 WM_TIMER 消息的重入    3 第1章定时器 1.1 创建定时器 请使用API函数 SetTimer 来创建定时器,其原型如下: UINT SetTimer(HWND hWnd,UINT nIDEvent,UINT uElapse,TIMERPROC lpTimer

4.内核定时器的使用

下面代码是本人在项目中遇到使用定时器的一个例子,可以编译运行正确的结果 1 #include <linux/timer.h> 2 3 struct timer_list timer; //内核定时器全局变量 4 5 void timer_init() //放在适当的地方,以便被正确地调用 6 { 7 timer.data = 0; 8 timer.expires = jiffies + 1*HZ; 9 timer.function = (int* )timer_handler; 10 add_

Spring 定时器 定时访问数据库并发送邮件

我这里有两个案例的方法: 第一种:使用Spring quartz: 我这里使用的jar:spring-context-support.jar.quartz-1.6.5.jar ==============applicationContext.xml配置================= <!--定时器service-->    <bean id="timerTaskServices" class="com.gzbugu.service.TimerTaskSe

JAVA之Timer定时器

1.原理 JDK中,定时器任务的执行需要两个基本的类:java.util.Timer;java.util.TimerTask; java.util.Timer定时器,实际上是个线程,定时调度所拥有的TimerTasks.一个TimerTask实际上就是一个拥有run方法的类,需要定时执行的代码放到run方法体内,TimerTask一般是以匿名类的方式创建. 要运行一个定时任务,最基本的步骤如下:1.建立一个要执行的任务TimerTask.2.创建一个Timer实例,通过Timer提供的sched