Angular1中的超时处理

使用$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

你可能感兴趣的:(Angular,Ionic)