【问题标题】:Angular $http config timeoutAngular $http 配置超时
【发布时间】:2016-10-30 03:11:57
【问题描述】:
如 Angular 文档中所述,
timeout – {number|Promise} – 以毫秒为单位的超时,或承诺在解决时应中止请求。
现在我将超时设置为承诺,所以我可以通过promise.resolve() 手动取消请求。
现在,我还想让它能够配置超时值,而不是将请求超时设置为 120 秒。
如何在不影响现有取消请求功能的情况下进行配置?
【问题讨论】:
标签:
javascript
angularjs
httprequest
angular-promise
angular-http
【解决方案1】:
你可以这样做
$scope.qPromiseCall = function()
{
var timeoutPromise = $timeout(function()
{
//aborts the request when timed out
canceler.resolve();
console.log("Timed out");
}, 250);
//we set a timeout for 250ms and store the promise in order to be cancelled later if the data does not arrive within 250ms
var canceler = $q.defer();
$http.get("data.js", {timeout: canceler.promise} )
.success(function(data)
{
console.log(data);
$timeout.cancel(timeoutPromise);
//cancel the timer when we get a response within 250ms
});
}
更多详情请看
Setting a timeout handler on a promise in angularjs
@Khanh TO 的第一个回答