Reputation: 221
I want to be able to abort asynchronous XMLHttpRequests which spend a long time waiting for results from the server. Because these requests can be very long, setting a timeout before the XHR is sent is not appropriate.
I can call XMLHttpRequest.abort() but while this does prevent the ready state change handlers from being called, it does not appear to close the connection to the server, so the server continues working (observed in both Firefox and Chrome). It appears that the server connection is not closed until data is finally received from the server.
The spec says that the timeout attribute on XHR can be set at any time, even after fetching has started: "This implies that the timeout attribute can be set while fetching is in progress. If that occurs it will still be measured relative to the start of fetching." However, setting this attribute immediately before or after calling abort() for a XHR which is waiting for data from the server appears to have no effect in Firefox 26 or Chrome 31. The connection stays open until the server has finished its work and sends response headers.
Is this feature supported in any browsers? Or am I misunderstanding something? I am testing using Javascript on the client side and a Java servlet on the server side, so no other frameworks getting in the way.
Upvotes: 3
Views: 2460
Reputation: 11245
You can find support here. Also note that in IE you needs add timeout
value after the open
method call. Example:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {/*code*/};
xhr.open(/*arguments*/);
xhr.timeout = 4000;
xhr.ontimeout = function () { /*Timeout handler*/ }
xhr.send(null);
Upvotes: 1