How to set a global http timeout in AngularJs

前端 未结 4 1597
予麋鹿
予麋鹿 2020-11-27 02:23

I know I can set a timeout each and every time:

$http.get(\'path/to/service\', {timeout: 5000});

... but I want to set a global timeout to

4条回答
  •  广开言路
    2020-11-27 03:08

    I've the same requirement and I m using AngularJS 1.0.7. I've come up with the below code as none of the above solutions seems feasible for me (feasible in the sense I want timeout to be global at one place). Basically, I m masking the original $http methods and adding timeout for each $http request and overriding other shortcut methods, like get, post, ... so that they'll use the new masked $http.

    JSFiddle for below code:

    /**
     * @name ngx$httpTimeoutModule
     * @description Decorates AngularJS $http service to set timeout for each
     * Ajax request.
     * 
     * Implementation notes: replace this with correct approach, once migrated to Angular 1.1.5+
     * 
     * @author Manikanta G
     */
    ;(function () {
        'use strict';
    
        var ngx$httpTimeoutModule = angular.module('ngx$httpTimeoutModule', []);
    
        ngx$httpTimeoutModule.provider('ngx$httpTimeout', function () {
            var self = this;
            this.config = {
                timeout: 1000 // default - 1 sec, in millis
            };
    
            this.$get = function () {
                return {
                    config: self.config
                };
            };
        });
    
        /** 
         * AngularJS $http service decorator to add timeout
         */
        ngx$httpTimeoutModule.config(['$provide',  function($provide) {
    
            // configure $http provider to convert 'PUT', 'DELETE' methods to 'POST' requests
            $provide.decorator('$http', ['$delegate', 'ngx$httpTimeout', function($http, ngx$httpTimeout) {
                // create function which overrides $http function
    
                var _$http = $http;
    
                $http = function (config) {
                    config.timeout = ngx$httpTimeout.config.timeout;
                    return _$http(config);
                };
                $http.pendingRequests = _$http.pendingRequests;
                $http.defaults = _$http.defaults;
    
                // code copied from angular.js $HttpProvider function
                createShortMethods('get', 'delete', 'head', 'jsonp');
                createShortMethodsWithData('post', 'put');
    
                function createShortMethods(names) {
                    angular.forEach(arguments, function(name) {
                        $http[name] = function(url, config) {
                            return $http(angular.extend(config || {}, {
                                method : name,
                                url : url
                            }));
                        };
                    });
                }
    
                function createShortMethodsWithData(name) {
                    angular.forEach(arguments, function(name) {
                        $http[name] = function(url, data, config) {
                            return $http(angular.extend(config || {}, {
                                method : name,
                                url : url,
                                data : data
                            }));
                        };
                    });
                }
    
                return $http;
            }]);
    
        }]);
    
    })();
    

    Add dependency on the above module, and configure the timeout by configuring ngx$httpTimeoutProvider, like below:

    angular.module('App', ['ngx$httpTimeoutModule']).config([ 'ngx$httpTimeoutProvider', function(ngx$httpTimeoutProvider) {
        // config timeout for $http requests
        ngx$httpTimeoutProvider.config.timeout = 300000; // 5min (5 min * 60 sec * 1000 millis)
    
    } ]);
    

提交回复
热议问题