I'm looking on the web, but documentation is hard to come by. We all know the basic AJAX call using the browser's built-in XMLHttpRequest
object (assume a modern browser here):
var xmlHttp = new XMLHttpRequest(); // Assumes native object xmlHttp.open("GET", "http://www.example.com", false); xmlHttp.send(""); var statusCode = xmlHttp.status; // Process it, and I'd love to know if the request timed out
So, is there a way that I can detect that the AJAX call timed out by inspecting the XMLHttpRequest object in the browser? Would I be advised to do something like window.setTimeout(function() { xmlHttp.abort() }, 30000);
?
Thanks!
-Mike
Session timeout has been a very common feature in Ajax-based web applications. In responsive interface, the programmer needs to delay the ajax request to achieve some task before the response. This can be achieved by using jQuery setTimeout() function.
The XMLHttpRequest. timeout property is an unsigned long representing the number of milliseconds a request can take before automatically being terminated. The default value is 0, which means there is no timeout.
The default value is 0 , which means there is no timeout.
Some of the modern browsers (2012) do this without having to rely on setTimeout
: it's included in the XMLHttpRequest. See answer https://stackoverflow.com/a/4958782/698168:
var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { alert("ready state = 4"); } }; xhr.open("POST", "http://www.service.org/myService.svc/Method", true); xhr.setRequestHeader("Content-type", "application/json; charset=utf-8"); xhr.timeout = 4000; xhr.ontimeout = function () { alert("Timed out!!!"); } xhr.send(json);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With