Note that there are some explanatory texts on larger screens.

plurals
  1. POCan XHR trigger onreadystatechange multiple times with readyState=DONE?
    primarykey
    data
    text
    <p>The <a href="http://www.w3.org/TR/XMLHttpRequest/#event-xhr-readystatechange" rel="noreferrer">W3C spec</a> suggest following implementation: Some simple code to do something with data from an XML document fetched over the network:</p> <pre><code>function processData(data) { // taking care of data } function handler() { if(this.readyState == this.DONE) { if(this.status == 200 &amp;&amp; this.responseXML != null &amp;&amp; this.responseXML.getElementById('test').textContent) { // success! processData(this.responseXML.getElementById('test').textContent); return; } // something went wrong processData(null); } } var client = new XMLHttpRequest(); client.onreadystatechange = handler; client.open("GET", "unicorn.xml"); client.send(); </code></pre> <p>Is this implementation really correct? </p> <p>During debug I found cases when the readystatechanged event handler is called more than once in a row with the same value of readyState == 4. I guess this behavior is correct, as specs says that each change of state must fire the event, and that readyState must always be equal to current state, so if several events pile up in the events queue, it's quite obvious, that one will get multiple calls with readyState == 4.</p> <p><a href="http://jsfiddle.net/44b3P/" rel="noreferrer">http://jsfiddle.net/44b3P/</a> -- this is the above example augmented with debugger call to pause execution just after the request is send, and with alert() in the processData. Once you unpause the execution you will get 3 alerts.</p> <p>This example from w3c seems to be copy&amp;pasted in multiple places in the web -- in particular OpenSocial seems to handle xhr events this way. Is it correct?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload