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));
}
} });

angularjs 定时器 销毁的更多相关文章

  1. vue组件里定时器销毁问题

    我在a页面写一个定时,让他每秒钟打印一个1,然后跳转到b页面,此时可以看到,定时器依然在执行.这样是非常消耗性能的.如下图所示: 解决方法1: 首先我在data函数里面进行定义定时器名称: data( ...

  2. AngularJS定时器任务

    由于项目需要监测用户在线时长,所以用定时器来实现. /*计算在线时长,一分钟执行一次*/ var stopEvent = $interval(function(){ //每分钟执行一次定时任务 $sc ...

  3. AngularJS - 定时器 倒计时例子

    <body> <div ng-app="myApp"> <div ng-controller="firstController"& ...

  4. Windows定时器

    目录 第1章定时器    1 1.1 创建定时器    1 1.2 销毁定时器    1 1.3 定时器的运作    1 1.3.1 产生WM_TIMER消息    1 1.3.2 分发WM_TIME ...

  5. spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象

    最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...

  6. MFC定时器使用

    MFC定时器实现方法 方法一:CWnd类提供的成员函数SetTimer实现定时器功能,只能在CWnd类或其派生类中调用. 方法二:Windows API函数SetTimer来实现. MFC定时器 启动 ...

  7. angularjs编码实践

    AngularJS 是制作 SPA(单页面应用程序)和其它动态Web应用最广泛使用的框架之一.我认为程序员在使用AngularJS编码时有一个大的列表点应该记住,它会以这样或那样的方式帮助到你.下面是 ...

  8. java web 项目中 简单定时器实现 Timer

    java web 项目中 简单定时器实现 Timer 标签: Java定时器 2016-01-14 17:28 7070人阅读 评论(0) 收藏 举报  分类: JAVA(24)  版权声明:本文为博 ...

  9. MFC Timer定时器

    知识点: 定时器Timer 创建定时器 销毁定时器 代码测试 一. 创建定时器 UINT SetTimer( HWND hWnd, // 指定关联定时器的窗口句柄,在MFC版将省略此参数 UINT n ...

随机推荐

  1. IO多路复用客户端-服务器模型

    IO多路复用服务器 -- 实现字符回射 服务器端 /************************************************************************* ...

  2. Linux kernel 编译问题记录【转】

    本文转载自:http://sunyongfeng.com/201701/programmer/linux/kernel_compile_fail.html 编译内核出现以下错误 Can't use ' ...

  3. codeforce diversity

    2017-08-25 14:59:34 writer:pprp 题意如下:给你一个串字符,再给你一个数字,表示在字符串中的各不相同的字符个数, 问你最少需要改变几个字符达到要求,不能达到要求就输出im ...

  4. maven项目中使用redis集群报错: java.lang.NumberFormatException: For input string: "7006@17006"

    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [redis.client ...

  5. POJ 1236 Network of School

    http://poj.org/problem?id=1236 题意: 给出一个图,至少要选多少个点才能遍历全图和至少需要添加多少边使得整个图是强连通. 思路: 强连通计算连通分量后缩点,计算入度为0的 ...

  6. js从一个select选择数据添加到另一个select(包括移除)

    一.实现效果 二.要求 1.选中左侧的菜单,点击“>>”,该菜单(1项或多项选中的)将添加到右侧菜单 2.选中右侧菜单,点击“<<”,则移除选中的菜单 3.点击“>> ...

  7. JavaScript内存泄漏知多少?

    垃圾回收解放了我们,它让我们可将精力集中在应用程序逻辑(而不是内存管理)上.但是,垃圾收集并不神奇.了解它的工作原理,以及如何使它保留本应在很久以前释放的内存,就可以实现更快更可靠的应用程序.在本文中 ...

  8. linux中的&&和&,|和||

    在linux中,&和&&,|和||介绍如下: &  表示任务在后台执行,如要在后台运行redis-server,则有  python a.py & && ...

  9. 如何调试nRF5 SDK

    本文将讲述Nordic nRF5 SDK的主要调试手段,以帮助大家快速定位问题,并解决问题.一般来说,你可以通过打log方式,IDE的debug模式,SDK自带的app_error_check函数,以 ...

  10. UVALive-3713 Astronauts (2-SAT)

    题目大意:有三个任务A.B.C,n个已知年龄的人.A任务只能被年龄不小于平均年龄的人做,B任务只能被平均年龄以下的人做,C任务不限,相互讨厌的两个人不能做同一件任务,现在已知厌恶关系,求一种任务分配方 ...