關鍵代碼:
// 定義一個定時器, 設置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