Axios请求超时 [英] Timeout on Axios Requests

查看:232
本文介绍了Axios请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的网站目前具有过滤功能,可根据要过滤的内容通过axios提取新数据.

Our site currently has a filter feature that fetches new data via axios depending on what is being filtered.

问题在于过滤器是实时完成的,通过react进行的每项更改都会导致axios请求.

The issue is that the filter is done on real time and every change made via react causes an axios request.

是否有办法在axios请求上设置超时,以便我仅获取最后一个状态?

Is there a way to put a timeout on the axios request so that I only fetch the last state?

推荐答案

在这种情况下,我建议在用户输入指定毫秒后使用debounce触发API调用.

I would suggest using debounce in this case to trigger API call after a specified millisecond of user input.

但是,以防万一您需要在 axios 调用期间添加超时,可以实现-

But just in case you need to add a timeout during axios call, this can be achieved like -

instance.get('/longRequest', {
  timeout: 5000
});

这篇关于Axios请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆