$timeout()定时器
非常不幸的一点是,人们似乎常常将AngularJS中的$timeOut()函数看做是一个内置的、无须在意的函数。但是,如果你忘记了$timeOut()的回调函数将会造成非常不好的影响,你可能会因此遇到代码莫名其妙的出现问题,或者无端抛出一个错误甚至是一遍一遍的重复对的你的服务器进行$http请求这些诡异的情形。管理好你的$timeOut定时器的小技巧就是在$destory事件中将它们取消。
和javascript中原生的setTimeout()以及setInterval()函数不同,AngularJS中的$timeOut()函数会返回一个promise。和其他的promise一样,你可以绑定$timeOut的resolved和rejected时间。然而更重要的是,你可以通过将这个promise传递给$timeOut.cancel()方法来取消掉潜在的定时器。
在一个AngularJS应用中,这一点非常的重要,因为定时器可以结束执行那些不再与应用状态和用户界面相关的代码。最佳情形中,这些过程都会悄悄的发生;在不好的情况下,它会引起不可预测的行为并导致很差的用户体验。为了让应用顺畅的运行,你应该总是把握好你的$timeOut定时器;你需要在相应的控制器和指令接收到$destory事件时调用$timeOut.cancel()方法。
为了更加清楚的说明这点,下面的这个例子将会有一些DOM元素通过ngSwitch/ngSwitchWhen指令来创建或销毁。注意到当$destory事件被触发时(在这里的例子中是位于指令中),我们将取消当前的定时器:
<!doctype html>
<html ng-app="Demo" ng-controller="DemoController">
<head>
<meta charset="utf-8" />
<title>
Don't Forget To Cancel $timeout Timers In Your $destroy Events In AngularJS
</title>
</head>
<body>
<h1>
Don't Forget To Cancel $timeout Timers In Your $destroy Events In AngularJS
</h1>
<p>
<a href="#" ng-click="toggle()">Toggle Section</a>
</p>
<div ng-switch="section">
<p ng-switch-when="happy" bn-directive>
Oh sweet!
</p>
<p ng-switch-when="sad" bn-directive>
Oh noes!
</p>
</div>
<!-- Load jQuery and AngularJS. -->
<script
type="text/javascript"
src="../../vendor/jquery/jquery-2.0.3.min.js">
</script>
<script
type="text/javascript"
src="../../vendor/angularjs/angular-1.0.7.min.js">
</script>
<script type="text/javascript">
//为我们的demo创建一个应用模块
var app = angular.module( "Demo", [] );
// -------------------------------------------------- //
// -------------------------------------------------- //
// 定义控制器
app.controller(
"DemoController",
function( $scope ) {
$scope.section = "happy";
//在toggle函数中改变section的值,以此在标记中显示/隐藏不同的部分
$scope.toggle = function() {
if ( $scope.section === "happy" ) {
$scope.section = "sad";
} else {
$scope.section = "happy";
}
};
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
//定义指令
app.directive(
"bnDirective",
function( $timeout ) {
//将用户界面的事件绑定到$scope上
function link( $scope, element, attributes ) {
//当timeout被定义时,它返回一个promise对象
var timer = $timeout(
function() {
console.log( "Timeout executed", Date.now() );
},
2000
);
//将resolve/reject处理函数绑定到timer promise上以确保我们的cancel方法能正常运行
timer.then(
function() {
console.log( "Timer resolved!", Date.now() );
},
function() {
console.log( "Timer rejected!", Date.now() );
}
);
//当DOM元素从页面中被移除时,AngularJS将会在scope中触发$destory事件。这让我们可以有机会来cancel任何潜在的定时器
$scope.$on(
"$destroy",
function( event ) {
$timeout.cancel( timer );
}
);
}
//返回指令的配置
return({
link: link,
scope: false
});
}
);
</script>
</body>
</html>
一般来说,放任$timeOut定时器可能不会有多大的影响。但是最好的做法是将它cancel掉,因为它有可能会造成很差的用户体验(或者想你的服务器发出不必要的请求)。这一点对于控制器和指令来说同样重要。
随机推荐
- Cisco路由器端口
Cisco的设备管理有很多种方式,如Console.HTTP.TTY.VTY或其它网管软件,但我们远程管理较为常用的一种方式肯定是VTY方式. VTY在Cisco的不同系列产品中,都有一定数量的VTY ...
- DOCKER 简单操作
docker ps -a 列出所有镜像 docker start docder_name dcoker start docker_id 运行某个镜像 docker restart docker_nam ...
- CSS3的transform属性
CSS3的一些属性可能比较新,有一些书从国外翻译到国内的时间上会延缓1-2年.所以有一些东西还需要及时整理. 下面说一下CSS3的一个属性:transform 其实字面上的意思已经很明显了就是变化,变 ...
- openstack-ansible Chapter 4. Deployment configuration
Initial environment configuration Copy the contents of the /opt/openstack-ansible/etc/openstack_depl ...
- Eureka、Ribbon、Feign常见问题及解决
1.Eureka常见问 1.1.Eureka Enviroment 的配置 eureka.enviroment=product 参考 https://github.com/Netflix/eureka ...
- Spring Boot入门——JDBCTemplate使用及其相关问题解决
1.在pom.xml文件中引入相应依赖 <!-- mysql依赖 --> <dependency> <groupId>mysql</groupId> & ...
- jquery jsonp模版
$.ajax({ dataType: "jsonp", url: "http://www.b.com/b.php", jsonp: "callback ...
- java: BufferedReader简单的菜单
BufferedReader简单的菜单 Menu boolean flag; public Menu() { while(true) { this.show(); } } public void sh ...
- 将一个jar包放到linux下定时执行
将一个jar包放到linux下定时执行 1.在dbtodb文件夹下新建一个dbtodb.sh,脚本内容为: #!/bin/bash cd /usr/dbtodb/ java -jar dbtodb.j ...
- ural 2012 About Grisha N.(水)
2012. About Grisha N. Time limit: 1.0 secondMemory limit: 64 MB Grisha N. told his two teammates tha ...