Angular1中的超时处理

来源:互联网 发布:白金数据 magnet 编辑:程序博客网 时间:2024/05/29 16:56

使用$http在请求访问中处理超时的代码:

关键代码

// 定义一个定时器, 设置5s为请求超时时间var timer = $timeout(function () {   console.log('登录超时!'); // 模拟提示信息},5000);// post的数据var postData = {"name":"ng", "password":"111111"};// 请求$http.post('your-login-url', postData, {"timeout": timer})     .success(function(data){        // storage and jump    })    .error(function(data){        // tips here    })    .finally(function(){        $timeout.cancel(timer); // 移除定时器    });

说明

  • https://docs.angularjs.org/api/ng/service/$http
  • 具体在都Api上有,不常用的功能,只要查查API就可以了。主要思路是在finally中结束定时器的操作。详情查看:$http(config); 中的Arguments
原创粉丝点击