Categories
Uncategorized

How to set $http timeout in AngularJs

http://

Global Timeout
To set a global http timeout in angularjs, we need to use the following code, which takes time in milliseconds to override the default http timeout value.

angular.module('myApp',[])
  .factory('timeoutHttpIntercept', function ($rootScope, $q) {
    return {
      'request': function(config) {
        config.timeout = 1 * 60 * 1000; 
                      // 1 minute or 60 seconds or 60,000 milliseconds
       return config; } 
      }; 
    });

angular.module('myApp')
  .config(function($routeProvider) { $httpProvider.interceptors.push('timeoutHttpIntercept');  });

Call specific Timeout
To specifically set a custom timeout for a particular http call that you make, you can include it as part of the config object.

var app = angular.module('myApp', []);
app.controller('myCtrl',   function($scope, $http) {

// 1 minute or 60 seconds or 60,000 milliseconds
var config = {
          method : "GET",
          url : "https://www.example.com/post",
          timeout: 1 * 60 * 1000
     };

$http(config)
        .then(
           function mySuccess(response) {
            $scope.myWelcome = response.data;
           },
           function myError(response)   {
             $scope.myWelcome =   response.statusText;
         });
});

                                                         or

var app = angular.module('myApp', []);
app.controller('myCtrl',   function($scope, $http) {
    
   $http({
          method : "GET",
          url : "https://www.example.com/post",
          timeout: 1 * 60 * 1000
      })
        .then(
            function mySuccess(response) {
               $scope.myWelcome = response.data;
            },
            function myError(response)   {
               $scope.myWelcome = response.statusText;
          });
});