AbortSignal: timeout() static method

Baseline 2024
Newly available

Since April 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Note: This feature is available in Web Workers.

The AbortSignal.timeout() static method returns an AbortSignal that will automatically abort after a specified time.

The signal aborts with a TimeoutError DOMException on timeout.

The timeout is based on active rather than elapsed time, and will effectively be paused if the code is running in a suspended worker, or while the document is in a back-forward cache ("bfcache").

To combine multiple signals, you can use AbortSignal.any(), for example, to directly abort a download using either a timeout signal or by calling AbortController.abort().

Syntax

js
AbortSignal.timeout(time)

Parameters

time

The "active" time in milliseconds before the returned AbortSignal will abort. The value must be within range of 0 and Number.MAX_SAFE_INTEGER.

Return value

An AbortSignal.

The signal will abort with its AbortSignal.reason property set to a TimeoutError DOMException on timeout, or an AbortError DOMException if the operation was user-triggered.

Examples

Below is an example showing a fetch operation that will timeout if unsuccessful after 5 seconds. Note that this may also fail if the method is not supported, if a browser "stop" button is pressed, or for another reason.

js
const url = "https://path_to_large_file.mp4";

try {
  const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
  const result = await res.blob();
  // …
} catch (err) {
  if (err.name === "TimeoutError") {
    // This exception is from the abort signal
    console.error("Timeout: It took more than 5 seconds to get the result!");
  } else if (err.name === "AbortError") {
    // This exception is from the fetch itself
    console.error(
      "Fetch aborted by user action (browser stop button, closing tab, etc.",
    );
  } else if (err.name === "TypeError") {
    console.error("AbortSignal.timeout() method is not supported");
  } else {
    // A network error, or some other problem.
    console.error(`Error: type: ${err.name}, message: ${err.message}`);
  }
}

Specifications

Specification
DOM
# ref-for-dom-abortsignal-timeout①

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
timeout() static method
Chrome – Partial support
Chrome 103 – 123 (Release date: 2022-06-21)
footnote Partial support
footnote Always aborts with an AbortError on timeout, not a TimeoutError.
Chrome – Full support
Chrome 124 (Release date: 2024-04-16)
footnote Full support
Edge – Partial support
Edge 103 – 123 (Release date: 2022-06-23)
footnote Partial support
footnote Always aborts with an AbortError on timeout, not a TimeoutError.
Edge – Full support
Edge 124 (Release date: 2024-04-18)
footnote Full support
Firefox – Full support
Firefox 100 (Release date: 2022-05-03)
footnote Full support
Opera – Partial support
Opera 89 – 109 (Release date: 2022-07-07)
footnote Partial support
footnote Always aborts with an AbortError on timeout, not a TimeoutError.
Opera – Full support
Opera 110 (Release date: 2024-05-14)
footnote Full support
Safari – Full support
Safari 16 (Release date: 2022-09-12)
footnote Full support
Chrome Android – Partial support
Chrome Android 103 – 123 (Release date: 2022-06-21)
footnote Partial support
footnote Always aborts with an AbortError on timeout, not a TimeoutError.
Chrome Android – Full support
Chrome Android 124 (Release date: 2024-04-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 100 (Release date: 2022-05-03)
footnote Full support
Opera Android – Partial support
Opera Android 71 – 81 (Release date: 2022-09-16)
footnote Partial support
footnote Always aborts with an AbortError on timeout, not a TimeoutError.
Opera Android – Full support
Opera Android 82 (Release date: 2024-05-02)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16 (Release date: 2022-09-12)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 20 – 26 (Release date: 2023-02-10)
footnote Partial support
footnote Always aborts with an AbortError on timeout, not a TimeoutError.
Samsung Internet – Full support
Samsung Internet 27 (Release date: 2024-11-06)
footnote Full support
WebView Android – Partial support
WebView Android 103 – 123 (Release date: 2022-06-21)
footnote Partial support
footnote Always aborts with an AbortError on timeout, not a TimeoutError.
WebView Android – Full support
WebView Android 124 (Release date: 2024-04-16)
footnote Full support
WebView on iOS – Full support
WebView on iOS 16 (Release date: 2022-09-12)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1.20 (Release date: 2022-03-17)
footnote Full support
Node.js – No support
Node.js 16.14 – 16.17 (Release date: 2022-02-08)
footnote Removed in 17 and later
Node.js – Full support
Node.js 17.3 (Release date: 2021-12-17)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.