Angularjs中的超时处理

关键代码:

// 定义一个定时器, 设置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); // 移除定时器
    });

说明

posted @ 2018-07-31 16:42  tianyou_songyk  阅读(1642)  评论(0编辑  收藏  举报