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); // 移除定时器 });

说明