使用 fetch 函数时可以在 options 参数中设置超时时间。可以使用 AbortController 来控制超时,示例代码如下:
const controller = new AbortController();
const signal = controller.signal;
setTimeout(() => {
controller.abort();
}, 5000);
fetch(url, { signal }).then(response => {
// do something with the response
}).catch(error => {