javascript - XMLHttpRequest timeout case - onreadystatechange executes before ontimeout? -
i using xmlhttprequest make asynchronous requests. have
xhr.onreadystatechange = function () { if (xhr.readystate === 4) {//handle if state has completed if (xhr.status === 200) { successhandler.call(context, xhr.responsetext); } else { console.warn('onreadystatechange:' + new date().gettime()); failurehandler.call(context, xhr.status, xhr.statustext); } } }; xhr.ontimeout = function () { console.warn('ontimeout: ' + new date().gettime()); failurehandler.call(context, 0, 'timeout'); };
i forcing timeout using network throttling. observe both onreadystatechange
, ontimeout
callbacks called in case of timeouts, onreadystatechange
preceding ontimeout
. under impression 1 of 2 executed. using chrome. know if expected?
the modern solution use appropriate xmlhttprequest events: onload , onerror if want handle these cases separately, onloadend if wish detect http request completion regardless of success, , ontimeout timeouts. when timeout occurs, ontimeout called, not onloadend. onreadystatechange looks it's best left die quietly. full set of events can found here:
http://www.w3.org/tr/xmlhttprequest/#events
since onreadystatechange→ontimeout sequence doesn't appear afflict ie 8, purposes i'm happy following: use onreadystatechange in ie 8, , onloadend in firefox/modern msie (using following test see if event defined: if ("onloadend" in httpobj) ...).
this internal system, suffices me; op, more testing may required.
Comments
Post a Comment