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. xml简单介绍及libmxml编程

    今天我们来简单介绍一下,关于xml的一些内容,包括自己编写一些程序进行生成和解析. 首先我们我们还是从xml的演化历史来了解一下它吧. 历史演化 GML: 在20世纪60年代为了促进数据交换和操作,通 ...

  2. RN中有两种方式使用全局变量

    1.通过导入导出文件的方式 新建constants.js文件 const object = { website:'http://www.hao123.com', name:'好123', }; exp ...

  3. caffe平台快速搭建:caffe+window7+vs2013

    caffe平台快速搭建:caffe+window7+vs2013 1.caffe-master下载 采用微软提供Windows工具包(caffe-master),下载地址:https://github ...

  4. heartbeat 编译安装配置

    一.heartbeat介绍 heartbeat是HA高可用集群的一个重要组件,heartbeat实现了资源转移和心跳信息传递.它的常用组合方式为heartbeat v1,heartbeat v2+cr ...

  5. PAT1071. Speech Patterns (25)

    题目要求的Word定义 Here a "word" is defined as a continuous sequence of alphanumerical characters ...

  6. 《Django By Example》

    <Django By Example>第六章 中文 翻译 (个人学习,渣翻) 书籍出处:https://www.packtpub.com/web-development/django-ex ...

  7. HTop 防止进程重复显示

    按F2 选择 Display options 选择 Hide userland threads 比Top更加好用!

  8. springboot项目属性配置及注意事项

    在idea编辑器建的springboot项目中的resources包下的application.properties这个就是配置文件. 另外配置文件的文件名还可以是application.yml,在r ...

  9. tensorflow 模型保存

    1.首先 saver = tf.train.Saver(max_to_keep=1)新建一个saver,max_to_keep是说只保留最后一轮的训练结果 2.使用save方法保存模型 saver.s ...

  10. (转)浅谈SQL Server 对于内存的管理

    简介 理解SQL Server对于内存的管理是对于SQL Server问题处理和性能调优的基本,本篇文章讲述SQL Server对于内存管理的内存原理. 二级存储(secondary storage) ...