timeout 1


本想通过$httpProvider的defaults属性配置timeout时间, defaults中没有这个属性.
https://docs.angularjs.org/api/ng/provider/$httpProvider#defaults

timeout 2


在拦截器中为request的config配置timeout, 在response中根据xhrStatus=timeout来处理超时. 奈何response中没找到这个xhrStatus
![](https://images2018.cnblogs.com/blog/213419/201804/213419-20180401112302128-243653749.png)

官网里说status=-1表示请求被取消, timeout时就会取消请求, 那就用这个来判断吧

https://docs.angularjs.org/api/ng/service/$http#$http-returns



代码


```
(function() {

angular.module("mobile")

.factory('HttpInterceptor', ['$q',
function($q) {
return {
request: function(config) {
// 设置超时时间, 毫秒
config.timeout = 1; if (config.url.search("service/") < 0)
return config;
if (localStorage.getItem('token')) {
config.params = config.params || {};
config.params['token'] = localStorage.getItem('token');
config.params['t'] = new Date().getTime();
}
return config;
},
requestError: function(err) {
return $q.reject(err);
},
response: function(res) {
console.log('res: ',res); if(res.statusText=='timeout')
{
console.log('timeout: ',111);
}
if (res.data && res.data.code) {
switch (res.data.code) {
case '000':
return res
case '100':
// alert(res.data.msg)
// 账号未注册
localStorage.removeItem('token');
localStorage.removeItem('groupid');
return res
case '200':
// session 无效
console.log(res.data.msg)
localStorage.removeItem('token');
localStorage.removeItem('groupid');
window.location.href = '';
break
}
} return res
},
responseError: function(err) {
if (-1 === err.status) {
// 请求超时
alert('请求超时');
} else if (500 === err.status) {
// 处理各类自定义错误
} else if (501 === err.status) {
// ...
}
return $q.reject(err);
}
};
}
]) .config(['$httpProvider',
function($httpProvider) {
$httpProvider.interceptors.push('HttpInterceptor');
}
])

})();

angualrjs 配置超时时间的更多相关文章

  1. dubbo面试题之dubbo-client 和dubbo-server同时配置超时时间,是以哪个为准;

    根据官网表示: 以 timeout 为例,下图显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似: 方法级优先,接口级次之,全局配置再次之. 如果级别一样, ...

  2. mybatis配置sql超时时间

    mybatis如果不配置,默认超时时间是不做限制的.当系统慢sql很多时,势必会增加数据库压力,系统性能及稳定性降低.所以有必要要设置sql超时设置,下面配置超时时间是5分钟. 第一步:全局配置如下 ...

  3. Java发送邮件必带超时时间配置

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在线上遇到了一个发送邮件的问题,记录一下. 一.先说 ...

  4. 【Spring Cloud 源码解读】之 【如何配置好OpenFeign的各种超时时间!】

    关于Feign的超时详解: 在Spring Cloud微服务架构中,大部分公司都是利用Open Feign进行服务间的调用,而比较简单的业务使用默认配置是不会有多大问题的,但是如果是业务比较复杂,服务 ...

  5. Mybatis设置超时时间

    Mybatis设置超时时间 mybatis如果不指定,默认超时时间是不做限制的,默认值为0.mybatis sql配置超时时间有两种方法: 1.全局配置 在mybatis配置文件的settings节点 ...

  6. SpringCloud之Feign 负载均衡请求超时时间

    版本声明: SpringCloud:Greenwich.SR4 SpringBoot:2.1.9.RELEASE Feign调用服务的默认时长是1秒钟,也就是如果超过1秒没连接上或者超过1秒没响应,那 ...

  7. config文件中可以配置查询超时时间

    web.config配置数据库连接 第一种:获取连接字符串 首先要定义命名空间 system.configuration 1.  string connstr= string constr = Con ...

  8. GRUB2配置详解:默认启动项,超时时间,隐藏引导菜单,配置文件详解,图形化配置

    配置文件详解: /etc/default/grub # 设定默认启动项,推荐使用数字 GRUB_DEFAULT=0 # 注释掉下面这行将会显示引导菜单 #GRUB_HIDDEN_TIMEOUT=0 # ...

  9. Confluence 6 配置数据库查询超时时间

    如果数据库的查询时间太长同时你的应用程序显示没有响应,你可以配置数据库的查询超时时间.在默认情况下 Confluence 没有超时时间.希望配置数据库查询超时时间,在你的测试服务器上进行下面的操作: ...

随机推荐

  1. 今天一起探讨shiro实现账户同一时刻session唯一

    今天和同事在一起探讨shiro如何实现一个账户同一时刻只有一session存在的问题,下面小编把核心代码分享到博客园平台,需要的朋友参考下http://m.0834jl.com 今天遇到一个项目问题, ...

  2. day52 js--- bom dom

    本文转载自李文周博客,-----cnblog.liwenzhou.com dom官网资料: http://www.w3school.com.cn/htmldom/dom_methods.asp Jav ...

  3. forward 和redirect

    http://www.cnblogs.com/davidwang456/p/3998013.html

  4. Linux上iptables防火墙的基本应用教程

    iptables是Linux上常用的防火墙软件,下面vps侦探给大家说一下iptables的安装.清除iptables规则.iptables只开放指定端口.iptables屏蔽指定ip.ip段及解封. ...

  5. datatables出现横向滚动条

    datatables会扩大表单的宽度,设置为table-responsive 自适应的时候则会出现滚动条.

  6. JMeter快速入门

    今天的年会已过,仍然是空手而归,不过俺坚信能让生活稳定永远都是努力.由于隔壁组负责年会的抢红包项目,因而趁此机会把通过工具模拟高并发的知识补了补,通过和身边大师的交流,总算是对压力测试有了个简要的了解 ...

  7. hr相关的

    1.自我介绍? 2.为什么要离职?之前几家公司离职的原因分别是什么? 3.从上一家公司离职的原因? 4.目前就职的公司最大的收获是什么? 从上家公司到目前公司的最大收获是什么,要突出目标明确,在当前的 ...

  8. String、StringBuffer、StringBuilder的比较

    看String类的定义:public final class String...{private final char value[];} 看AbstractStringBuilder类的定义:abs ...

  9. ROWNUM = 1 to replace count(*)

    For a long time, I have been using the EXISTS clause to determine if at least one record exists in a ...

  10. 在UnrealEngine中用Custom节点实现径向模糊

    //input NotUse 为了开启SceneTextureLookup函数而连接的节点,但是不参与逻辑 //input UV 屏幕缓存的坐标坐标 //input Strength 力度 //inp ...