abort
Summary
Stops an asynchronous XMLHttpRequest in progress.
Method of apis/xhr/XMLHttpRequestapis/xhr/XMLHttpRequest
Syntax
.abort();
Return Value
No return value
Examples
function request() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost/test.xml", true);
xhr.send();
return xhr;
}
document.getElementById('submit').addEventListener('click', function () {
var xhr = request(),
submit = this,
cancel = document.getElementById('cancel');
function detach() {
submit.removeEventListener('click', canceling, false);
cancel.removeEventListener('click', canceling, false);
}
function canceling() {
detach();
xhr.abort();
}
// detach any remaining handlers after XHR finishes
xhr.addEventListener('load', detach, false);
// cancel if "Submit" is clicked again before XHR finishes
submit.addEventListener('click', canceling, false);
// cancel if "Cancel" is clicked
cancel.addEventListener('click', canceling, false);
}, false);
Notes
Calling abort resets the object; the readyState is changed to 0 (uninitialized). Calling it on an already aborted request throws an exception.
Related specifications
- W3C XMLHttpRequest Specification
- W3C Working Draft
Attributions
Microsoft Developer Network: Windows Internet Explorer API reference Article