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));
}
}
});
angularjs 定时器 销毁的更多相关文章
- vue组件里定时器销毁问题
我在a页面写一个定时,让他每秒钟打印一个1,然后跳转到b页面,此时可以看到,定时器依然在执行.这样是非常消耗性能的.如下图所示: 解决方法1: 首先我在data函数里面进行定义定时器名称: data( ...
- AngularJS定时器任务
由于项目需要监测用户在线时长,所以用定时器来实现. /*计算在线时长,一分钟执行一次*/ var stopEvent = $interval(function(){ //每分钟执行一次定时任务 $sc ...
- AngularJS - 定时器 倒计时例子
<body> <div ng-app="myApp"> <div ng-controller="firstController"& ...
- Windows定时器
目录 第1章定时器 1 1.1 创建定时器 1 1.2 销毁定时器 1 1.3 定时器的运作 1 1.3.1 产生WM_TIMER消息 1 1.3.2 分发WM_TIME ...
- spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象
最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...
- MFC定时器使用
MFC定时器实现方法 方法一:CWnd类提供的成员函数SetTimer实现定时器功能,只能在CWnd类或其派生类中调用. 方法二:Windows API函数SetTimer来实现. MFC定时器 启动 ...
- angularjs编码实践
AngularJS 是制作 SPA(单页面应用程序)和其它动态Web应用最广泛使用的框架之一.我认为程序员在使用AngularJS编码时有一个大的列表点应该记住,它会以这样或那样的方式帮助到你.下面是 ...
- java web 项目中 简单定时器实现 Timer
java web 项目中 简单定时器实现 Timer 标签: Java定时器 2016-01-14 17:28 7070人阅读 评论(0) 收藏 举报 分类: JAVA(24) 版权声明:本文为博 ...
- MFC Timer定时器
知识点: 定时器Timer 创建定时器 销毁定时器 代码测试 一. 创建定时器 UINT SetTimer( HWND hWnd, // 指定关联定时器的窗口句柄,在MFC版将省略此参数 UINT n ...
随机推荐
- DVWA安装
DVWA安装: 启动xampp下的apache中间件和mysql 将dvwa放到xampp下的htdocs目录下 在浏览器输入http://127.0.0.1/dvwa 即可使用啦! 还有owasp的 ...
- zsh + oh-my-zsh 默认shell
项目地址 zsh -----> http://www.zsh.orgoh-my-zsh ----> http://ohmyz.sh The last shell you’ll ever n ...
- 百度开源分布式id生成器uid-generator源码剖析
百度uid-generator源码 https://github.com/baidu/uid-generator snowflake算法 uid-generator是基于Twitter开源的snowf ...
- HTTP协议Keep-Alive模式详解和HTTP头字段总结
1.什么是Keep-Alive模式? 我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成 之后立即断开连接(HT ...
- ideal开发工具环境设置
ideal现在是很流行的一种java开发工具,不得不说,它真的很好用.当你使用的时候,需要设置环境. 首先,如果产生乱码,解决如下: 设置为UTF-8 如果项目是maven项目,设置如下: 也可以设置 ...
- visual studio中添加existing web site, website名字附带数字
用visual studio直接运行website项目 发现有一个conifg文件的配置,路径是solution同级目录下的.vs/config文件夹下有一个applicationhost.confi ...
- HTML5如何做横屏适配
在移动端中我们经常碰到横屏竖屏的问题,那么我们应该如何去判断或者针对横屏.竖屏来写不同的代码呢. 首先在head中加入如下代码: 1 <meta name="viewport" ...
- int数组的下标为n
int 类型的数组 先定义int n = 5: 定义int a[n] = {0}; 编译怎么不通过啊?n必须为常量如果想实现你说的,应该用宏#define n 5int a[n]={0};这样就可以了 ...
- Oracle中 如何用一个表的数据更新另一个表中的数据
准备阶段 1.建表语句: create table table1( idd varchar2(10) , val varchar2(20) ); create table table2( idd va ...
- 2-6-搭建FTP服务器实现文件共享
---------------------------------------------- 大纲: FTP服务:(文件传输协议---> File Transfer Protocol) 常见FT ...