提取API请求超时?

我有一个fetch-api POST要求:


   fetch(url, {

      method: 'POST',

      body: formData,

      credentials: 'include'

    })

我想知道默认的超时时间是多少?以及如何将其设置为3秒或不定秒的特定值?


白猪掌柜的
浏览 394回答 3
3回答

慕后森

使用中止语法,您将能够执行以下操作: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);})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript