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

$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. webpack学习笔记(1)--webpack.config.js

    主要的信息都是来自于下方所示的网站 https://webpack.docschina.org/configuration 从 webpack 4.0.0 版本开始,可以不用通过引入一个配置文件打包项 ...

  2. [Cerc2007]robotic sort

    splay区间反转练手题 #include <iostream> #include <cstdio> #include <algorithm> using name ...

  3. Vim配置持续记录

    1. 家目录创建.vimrc文件 set nu    # 设置行号 set tabstop=4 # tab制表符缩进 set autoindent # 自动缩进 set showmatch # 括号匹 ...

  4. SpringBoot 整合 Mybatis 进行CRUD测试开发

    今天来和大家分享下 Spring Boot 整合 MyBatis 的 CRUD 测试方法开发.因为 MyBaits 有两种开发形式,一种基于注解,一种基于 xml . SpringBoot配置文件也有 ...

  5. JavaScript响应式轮播图插件–Flickity

    简介 flickity是一款自适应手机触屏滑动插件,它的API参数很丰富,包括对齐方式.循环滚动.自动播放.是否支持拖动.是否开启分页.是否自适应窗口等. 在线演示及下载 演示地址 下载页面 使用方法 ...

  6. Problem 22

    Problem 22 Using names.txt (https://projecteuler.net/problem=22)(right click and 'Save Link/Target A ...

  7. Bootstrap关于表格

    1.Bootstrap为表格提供了1种基础样式和4种附加样式以及1个支持响应式的表格. ☑  .table:基础表格 ☑  .table-striped:斑马线表格 ☑  .table-bordere ...

  8. 提高生产力:文件和IO操作(ApacheCommonsIO-汉化分享)

    复制.移动.删除.比较.监控.文件读写 等文件和IO操作是编程中比较常用的功能.        幸运的是,Apache Commons IO等开源组件已经帮我们实现了.        我们可以不用重复 ...

  9. flask-sqlalchemy 配置 mysql (转载的文章)

    一.当然是把必备的包给安装上才行: Flask-SQLAlchemy pip install flask-sqlalchemy MySQL windows下64位压缩包的安装方式可以参考: http: ...

  10. Appendix B: Netsh Command Syntax for the Netsh Firewall Context

    11 out of 19 rated this helpful - Rate this topic Published: December 17, 2004 The following Netsh c ...