Тайм-аут Angular 1.5 с использованием HttpInterceptor
Все,
я пытаюсь настроить глобальный httpInterceptor, чтобы иметь настраиваемое всплывающее сообщение при появлении тайм-аута клиента, а не сервера. Я нашел эту статью:Angular $ http: установка обещания в конфигурации 'timeout' и преобразовал его в httpInterceptor, но он не работает должным образом и имеет странное поведение.
$provide.factory('timeoutHttpInterceptor', function ($q, $translate, $injector) {
var timeout = $q.defer();
var timedOut = false;
setTimeout(function () {
timedOut = true;
timeout.resolve();
}, 10000);
return {
request: function (config) {
config.timeout = timeout.promise;
return config;
},
response: function(response) {
return response;
},
responseError: function (config) {
if(timedOut) {
var toastr = $injector.get('toastr');
toastr.custom('network', $translate('title'), $translate('label'), { timeOut: 5000000000000, closeButton: true, closeHtml: '<button></button>' });
return $q.reject({
error: 'timeout',
message: 'Request took longer than 1 second(s).'
});
}
},
};
});