javascriptxmlhttprequestreadystateonreadystatechange

Are there useful uses of readyState different from "4" (complete) in a XHR.onreadystatechange callback?


Have you ever used an XHR object to intercept onreadystatechange with a readyState different from "4" (complete) ?

I'm curious to know if you ever trigger a function with the possible different values. I cannot imagine a real use of the others states. Are they somewhat useful to do something?

Can give some practical examples if any ?

I'm talking about these:


Solution

  • I use it on an intranet I have developed for notification purposes. By intercepting the state 3 I can notify the user that the request started.

    I also use it to time the requests transmission times. I display the time elapsed between states 3 and 4.

    Since I use MooTools I extended the Request class to fire the onStateChange event:

    var EarlyRequest = new Class({Extends: Request,
     onStateChange: function() {
      this.fireEvent("onStateChange", [this.xhr.readyState]);
      this.parent();
     }
    });
    

    On an additional note. The definitions of the states that you posted (from w3cschools) are misleading, these are clearer to me (from http://www.w3.org/TR/XMLHttpRequest/#states):