在反应中使用axios,如何指定get请求出错的时间限制?

using axios in react, how to specify a time limit for a get request to error out?

如果服务器在x秒内没有响应,那我想算一个错误。

如何指定 x 正在使用 axios?

试试这个代码:

// Create an instance using the config defaults provided by the library
// At this point the timeout config value is `0` as is the default for the library
const instance = axios.create();

// Override timeout default for the library
// Now all requests using this instance will wait 2.5 seconds before timing out
instance.defaults.timeout = 2500;

// Override timeout for this request as it's known to take a long time
instance.get('/longRequest', {
  timeout: 5000
});