Angular $http config timeout

Angular $http config timeout

本文关键字:timeout config http Angular      更新时间:2023-09-26

按照angular文档中的说明,

timeout - {number|Promise} -以毫秒为单位的超时时间,或者在解析请求时应该中止的承诺。

现在我将timeout设置为promise,所以我可以手动取消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
    });
  }

查看

在angularjs中为承诺设置超时处理程序

@Khanh TO的第一个回答