Java Reference
In-Depth Information
Ajax Timeouts
Ajax requests can take different amounts of time for a response, depending on the server to
which the request is made. This is why it's important to ensure the request is made asyn-
chronously, so that the browser avoids being locked up while waiting for the response. But
what if the server is down and the response never comes back?
The XMLHttpRequest object has a timeout property for setting the amount of time to wait
for a response. This is measured in milliseconds and is set to 0 by default, which indicates
no timeout period.
For example, to set a timeout of five seconds the following code can be used:
xhr.timeout = 5000;
If a response is not received after the specified amount of time, the request is automatically
terminated. Alternatively, a function can be assigned to the ontimeout property that will
be called when the request times out:
xhr.ontimeout = funciton() {
console.log("request timed out");
}
Note that both these properties need to be set before the xhr.send() method is called.
Search WWH ::




Custom Search