0

Imagine I have an array of XMLHttpRequest objects:

var scriptArray = []; // (filled with XMLHttpRequest objects all with the "readyState" unknown

And every XMLHttpRequest object has the following "load" event attached:

function handleLoadEvent(event) {
  // ignore status, I'll check for 404's, etc... in production

  // ignore eval, I'll use the <script> method in production
  window.eval($event.currentTarget.responseText); 

  // log that this object is loaded
  window.console.log('My script ran!');

  for(var i = 0; i < scriptArray.length; i++) {
    if(scriptArray[i].readyState === 4) {
      window.console.log('Is it possible that another XMLHttpRequest object "readyState" is "4" but it\'s "load" event has not fired?');
    }
  }
}

Is it possible that another XMLHttpRequest object "readyState" is "4" but it's "load" event has not fired?

In actual practice, this is difficult to test since objects load very quickly.

Andrew Bessa
  • 271
  • 3
  • 9
  • I suspect so. If several XHRs become ready at the same time, the `load` event will fire for the first one, but the others will have their `readystate` set. – Barmar May 25 '17 at 15:50
  • Have a look at https://stackoverflow.com/questions/9181090/is-onload-equal-to-readystate-4-in-xmlhttprequest – Chris Lear May 25 '17 at 15:52
  • I did see that post, but nothing was really clear. I'm speculating the answer to my question is "yes", it's possible "readyState" can be 4 and the "load" event has not fired yet. However, I want to know for sure. – Andrew Bessa May 25 '17 at 15:58
  • One useful test might be to add an onreadystatechange event, and see whether that gets fired before the load event. If so, you would have to answer yes to your question. – Chris Lear May 25 '17 at 15:59

0 Answers0