XMLHttpRequest: timeout プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

XMLHttpRequest.timeoutunsigned long 型で、リクエストが自動的に終了するまでの時間をミリ秒で示します。既定値は 0 で、タイムアウトが無いことを示します。 timeout は文書環境で利用される同期 XMLHttpRequest や InvalidAccessError の例外を投げるものに使用することはできません。タイムアウトとなった場合、 timeout イベントが発行されます。

メモ: ウィンドウを持つ同期リクエストでは、タイムアウトを使用することはできません。

タイムアウトは非同期リクエストで使用してください

js
const xhr = new XMLHttpRequest();
xhr.open("GET", "/server", true);

xhr.timeout = 2000; // ミリ秒単位の時間

xhr.onload = () => {
  // リクエスト完了。ここで処理を行います。
};

xhr.ontimeout = (e) => {
  // XMLHttpRequest がタイムアウト。ここで何かを行います。
};

xhr.send(null);

仕様書

Specification
XMLHttpRequest
# the-timeout-attribute

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
timeout

Legend

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

Full support
Full support
Has more compatibility info.