Is there any useful use of readyState other than "4" (full) in the XHR.onreadystatechange callback?

Have you ever used an XHR object to intercept onreadystatechange with a readyState other than "4" (full)?

I am curious to know if you ever run a function with various possible values. I can not imagine the real use of other states. Are they somewhat useful for something?

Can I give some practical examples, if any?

I say this :

  • 0: request not initialized
  • 1: server connection established
  • 2: received request
  • 3: processing request
  • 4: The request is complete and the response is ready.
+3
source share
1 answer

I use it on an intranet that I developed for notification purposes. Intercepting state 3, I can notify the user of the start of the request.

I also use it during request transmission time. I show the elapsed time 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();
 }
});

Additional note. The definitions of the states you posted (from w3cschools) are misleading, this is clearer for me (from http://www.w3.org/TR/XMLHttpRequest/#states ):

  • UNSENT (numerical value 0) object is constructed.

  • ( 1) open() . setRequestHeader(), send().

  • HEADERS_RECEIVED ( 2) ( ) , HTTP- . .

  • ( 3) .

  • ( 4) - (, ).

+2

Source: https://habr.com/ru/post/1781552/


All Articles