Xmlhttprequest Timeout / Abort Not Working As Expected?
Solution 1:
The problem seems to be that timeout
and ontimeout
aren't yet part of some implementations:
var hasTimeout = 'timeout'innewXMLHttpRequest(); // false
At least, it's not in Chrome 16 or Firefox 7. And, this should return true
given the requirements:
The
timeout
attribute must return its value. Initially its value must be zero.
Both have been part of the XHR 2 spec since Sept 7, 2010. But, as the "Level 2" spec has been been around since Feb 25, 2008 and is still a "Working Draft," there's not really a guarantee that any implementation would be up-to-date with the spec.
Without those available to you, you can try instead using onabort
and setTimeout
(as you stated in your comment):
// snip
to = function() {
attempt++;
if( attempt < 5)
send();
elseif( !silent) {
console.log("Request Timeout\nFailed to access "+url);
}
};
// snipvar send = function() {
if( loader && attempt != 0) {
loader.children[0].firstChild.nodeValue = "Error... retrying...";
loader.children[1].firstChild.nodeValue = "Attempt "+(attempt+1)+" of 5";
}
a = newXMLHttpRequest();
a.open("POST","/ajax/"+url,true);
a.onreadystatechange = rsc;
setTimeout(function () { /* vs. a.timeout */if (a.readyState < 4) {
a.abort();
}
}, 5000);
a.onabort = to; /* vs. a.ontimeout */
a.setRequestHeader("Content-Type","application/json");
a.send(data);
console.log('HTTP Requesting: %s', url);
};
// snip
Example: http://jsfiddle.net/AmQGM/2/ -- The ?delay=2
should finish, while the ?delay=10
expires its 5 tries.
Solution 2:
When running your code, I got the error c00c023f. When I googled it, it came up with this answer:
http://www.enkeladress.com/article.php/internetexplorer9jscripterror
This sounds very similar to what you're experiencing.
Here's a SO question which has the same problems, with a solution (also based on the above link, but with additional information): IE 9 Javascript error c00c023f
Solution 3:
Before aborting the request, try setting
a.onreadystatechange = function() {};
after that, also add a check around calling abort:
if( a.readyState > 0 && a.readyState < 4 ) {
a.abort();
}
Solution 4:
I had the same problem, it turned out to be the issue with PHP session. If the session is open, all other requests from the same session must wait.
<?php
session_write_close();
sleep(60);
touch("test/".uniqid());
die("Request completed.");
?>
This, of course, would not help you if you haven't started the session :)
Post a Comment for "Xmlhttprequest Timeout / Abort Not Working As Expected?"