项目中有用到定时器定时刷新页面的数据,在网上查看了一些资料,整理了一下,备忘。

$timeout

用法如下$timeout(fn,[delay],[invokeApply]);

  • fn:一个将被延迟执行的函数。
  • delay:延迟的时间(毫秒)。
  • invokeApply:如果设置为false,则跳过脏值检测,否则将调用$apply。

方法cancel(promise); 
promise:$timeout函数的返回值。

具体使用:在项目中用到的其中一处是键入字符然后自动发送请求查询,如果每键入一个字符即发送一次请求,在数据库表庞大的时候,数据库肯定会有意见了。这时候就需要用到延时查询了,还需要结合$watch,具体使用如下:

var timeout;
$scope.$watch('idNo', function(newVal, oldVal) {
if(newVal != oldVal) {
if(timeout) {
$timeout.cancel(timeout);
}
timeout = $timeout(function() {
$http.get(url).success(function(data) {});
}, 800);
}
});

如上写法,if(newVal != oldVal){}是默认情况下不查询,键入字符后才会发送请求查询,若想默认情况下就查询则需要去掉该if判断。$timeout.cancel(timeout);是清除上次$itmeout返回的promise。

$interval

用法如下$interval(fn,delay,[count],[invokeApply],[Pass]);

  • fn:一个将被反复执行的函数。
  • delay:每次调用的间隔毫秒数值。
  • count:循环次数的数值,如果没设置,则无限制循环。
  • invokeApply:如果设置为false,则避开脏值检查,否则将调用$apply。
  • Pass:函数的附加参数。

方法cancel(promise); 
promise:$interval函数的返回值。 
具体使用

  1. 经常使用的是
function hello() {
...
console.log("hello world");
}
var timer = $interval(function(){
function hello() {}
},100);
timer.then(function() {
console.log("done");
});

以上是每100毫秒执行hello()函数,每执行完一次则调用then函数。

2. 控制循环的次数:var timer = $interval(function(){},100,10);,参数10则是限制定时器循环10次,若该参数没有定义则表示无数次循环。 
3. 清除interval定时器:通过‘interval.cancle(timer)`删除$interval返回的promise即可清除,而且必须要清除,否则会无限循环。在angular controller中只要开始执行定时任务,只要不清除则会一直执行,无论是否切换到其他的controller和页面,可能会导致不必要的错误。 
4. 项目中用到的完整实例:

// 定时器 定时刷新数据
var timer = $interval(
function() {
hello();//自己定义的每次需要执行的函数,也可以写一些其他的内容
},
5000
);
//当DOM元素从页面中被移除时,AngularJS将会在scope中触发$destory事件。
//这让我们可以有机会来cancel任何潜在的定时器。切换controller、页面后即可调用
$scope.$on(
"$destroy",
function() {
$interval.cancel( timer );
}
);

Angular 定时器$timeout和$interval关于定时刷新页面和发送请求的用法的更多相关文章

  1. Angular 定时器$timeout和$interval,延时调用

    项目中有用到定时器定时刷新页面的数据,在网上查看了一些资料,整理了一下,备忘. $timeout 用法如下:$timeout(fn,[delay],[invokeApply]); fn:一个将被延迟执 ...

  2. JS定时刷新页面及跳转页面

    JS定时刷新页面及跳转页面 Javascript 返回上一页1. Javascript 返回上一页 history.go(-1), 返回两个页面: history.go(-2); 2. history ...

  3. JSP简单练习-定时刷新页面

    <%@ page contentType="text/html; charset=gb2312" %> <%@ page import="java.ut ...

  4. 【jQuery】: 定时刷新页面

    <%@page import="qflag.ucstar.seatmonitor.manager.SeatMonitorManager"%><%@ page la ...

  5. 定时刷新页面SetInterval 和setTimeout -时间间隔可以动态设定

    JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操作. setTimeout("function& ...

  6. js定时刷新页面.

    //页面定时刷新.2017.09.27 $(document).ready(function () { self.setInterval(function () { var d = new Date( ...

  7. http-equiv="Refresh" 实现定时刷新页面

    ***.html自动跳转文件代码如下: <HTML> <HEAD><META http-equiv="Refresh" content="5 ...

  8. Vue使用定时器定时刷新页面

    1. 需求说明 在前端开发中,往往会遇到页面需要实时刷新数据的情况,给用户最新的数据展示. 2. 逻辑分析 如果需要数据实时更新,我们自然是需要使用定时器,不断的调用接口数据,会相对的消耗内存. 3. ...

  9. Ajax实现定时刷新页面

    function deleteValue(){ var refresh = function() { $.ajax({   type:'post',   url:'/Application/index ...

随机推荐

  1. Java中==和equals()的区别

  2. 图片base64格式转为file文件类型上传方法

    日常使用文件上传方式,都是通过input type='file'的文件选择框进行文件上传.但是会通过其他交互方式等到图片的base64格式进行上传.具体情况如下示意: 在项目开发中,需要进行照片采集, ...

  3. 15.Hibernate一对多双向关联映射+分页

    1.创建如下数据库脚本 --创建用户信息表 --编号,用户名,密码,年龄,性别,昵称,手机,地址,管理员,图像地址 create table users ( id ) primary key, use ...

  4. php去除h5标签

    function html2text($str){  $str = preg_replace("/<style .*?<\\/style>/is", " ...

  5. BZOJ 3119 Book (贪心+数学推导)

    手动博客搬家: 本文发表于20191029 22:49:41, 原地址https://blog.csdn.net/suncongbo/article/details/78388925 URL: htt ...

  6. Spring MVC学习总结(3)——Spring3 MVC详解

    DispatcherServlet 前置控制器 使用Spring MVC,配置DispatcherServlet是第一步.DispatcherServlet是一个Servlet,所以可以配置多个Dis ...

  7. ACdream 1735 输油管道

    输油管道 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 262144/131072KB (Java/Others)   Problem Des ...

  8. Java基础教程:tutorialspoint-spring mvc

    教程: 来自turorialspoint的Spring MVC 4.1.6教程(英文),官网:https://www.tutorialspoint.com/springmvc/index.htm 离线 ...

  9. [React] Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3

    getDerivedStateFromProps is lifecycle hook introduced with React 16.3 and intended as a replacement ...

  10. UVA 10025(数学)

     The ? 1 ? 2 ? ... ? n = k problem  The problem Given the following formula, one can set operators ' ...