I have an condition which has to hold before and after an XMLHttpRequest usage, but not during. Example:
var running = false; // condition
function go (callback)
{
running = true; // condition broken
var http = new XMLHttpRequest ();
http .open ("GET", url, true);
http .onreadystatechange = function ()
{
if (4 == this .readyState)
{
try
{
callback (this .responseText);
}
catch (e) {}
running = false; // condition restored
}
}
}
I need to guarantee that running will always eventually become false, regardless of how the XMLHttpRequest plays out.
Is this sufficient or do I need to check some other eventualities?
It looks like that this check is enough (http://www.w3.org/TR/XMLHttpRequest/). readyState is 4 if the data transfer has been completed or something went wrong during the transfer.
Make sure you set some reasonable value for timeout (the default is 0, that is no timeout).