Angular $http 配置超时
Angular $http config timeout
如 angular 文档中所述,
timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.
现在我正在设置超时来保证,所以我可以通过promise.resolve()
手动取消请求。
现在,我也想让它能够配置超时值,而不是让请求超时为 120 秒。
如何配置它而不影响现有的取消请求功能?
你可以这样做
$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 的第一个回答
如 angular 文档中所述,
timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.
现在我正在设置超时来保证,所以我可以通过promise.resolve()
手动取消请求。
现在,我也想让它能够配置超时值,而不是让请求超时为 120 秒。
如何配置它而不影响现有的取消请求功能?
你可以这样做
$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 的第一个回答