MDN self-hosted library · e01.org

XMLHttpRequestEventTarget: timeout event

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
const client = new XMLHttpRequest();
client.open("GET", "http://www.example.org/example.txt");
client.timeout = 5000;
client.ontimeout = () => {
  console.error("Timeout!!");
};

client.send();
js
client.addEventListener("timeout", () => {
  console.error("Timeout!!");
});
js
// In case of a timeout we hide the progress bar
// Note that this event can be listened to on the xhr object too
function errorAction(event) {
  progressBar.classList.remove("visible");
  log.textContent = `Upload failed: ${event.type}`;
}
xhr.upload.addEventListener("timeout", errorAction);