我有一个fetch-api POST要求: fetch(url, { method: 'POST', body: formData, credentials: 'include' })我想知道默认的超时时间是多少?以及如何将其设置为3秒或不定秒的特定值?
3 回答
慕后森
TA贡献1802条经验 获得超5个赞
使用中止语法,您将能够执行以下操作:
const controller = new AbortController();
const signal = controller.signal;
const fetchPromise = fetch(url, {signal});
// 5 second timeout:
const timeoutId = setTimeout(() => controller.abort(), 5000);
fetchPromise.then(response => {
// completed request before timeout fired
// If you only wanted to timeout the request, not the response, add:
// clearTimeout(timeoutId);
})
添加回答
举报
0/150
提交
取消