关键代码:
// 定义一个定时器, 设置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