We can manually set timeout to each $http via $http.get('url', {timeout: 5000}); Is it possible to set a global $http timeout once and it will apply to the whole application?
-
if you wrap your http calls in a service which does that then yes. or user some sort of $http interceptor service, also could be done... if you need examples let me knowJony-Y– Jony-Y2015-10-24 08:49:24 +00:00Commented Oct 24, 2015 at 8:49
-
@Jony-Y Thanks a lot for your comment. It would be nice if you could show an example how your suggestion can be done.user1995781– user19957812015-11-03 06:05:55 +00:00Commented Nov 3, 2015 at 6:05
-
pretty much like @NetSou showed in the answer... just add your $timeout there or what ever you want it to doJony-Y– Jony-Y2015-11-03 09:34:23 +00:00Commented Nov 3, 2015 at 9:34
Add a comment
|
1 Answer
You can use request http interceptor. Like this.
angular.module('myapp')
.factory('timeoutHttpInterceptor', function () {
return {
'request': function(config) {
config.timeout = 10000;
return config;
}
};
});
And then in .config inject $httpProvider and do this:
$httpProvider.interceptors.push('timeoutHttpInterceptor');
OR You can do like this also:
// this custom config could actually be a part of a more general app-level config
// so that you need to inject only one global config
myApp.value('http_defaults', {
timeout: 150
});
Here you can see more simple way Inject above http_defaultsin every controller were you are using $http. And set all default properties of http request.
myApp.controller('myCtrl', function ($scope, $http, http_defaults) {
// in case you need to change the default values for this specific request
// angular.extend(http_defaults, {timeout: 300});
$http.get("/xyz", http_defaults)
.success(success)
.error(error);
};
});
I would suggest to use first option.
1 Comment
demaniak
would you perhaps know what the default timeout is for $http service? Or is there no default timeout (wait for ever)?