动态配置axios超时

This commit is contained in:
buvta
2020-12-26 22:34:53 +08:00
parent 84e61acde7
commit 547dffb922

View File

@@ -15,7 +15,8 @@ var session = win.webContents.session
var ElectronProxyAgent = require('electron-proxy-agent')
// 请求超时时限
axios.defaults.timeout = 10000 // 可能使用代理,增长超时
//axios.defaults.timeout = 10000 // 可能使用代理,增长超时
const TIMEOUT = 6000
// 重试次数共请求3次
axios.defaults.retry = 2
@@ -23,6 +24,18 @@ axios.defaults.retry = 2
// 请求的间隙
axios.defaults.retryDelay = 1000
//使用请求拦截器动态调整超时
axios.interceptors.request.use(function (config) {
if (config.__retryCount === undefined) {
config.timeout = TIMEOUT
} else {
config.timeout = TIMEOUT * (config.__retryCount + 1)
}
return config
}, function (err) {
return Promise.reject(err)
})
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
return response