Quick Javascript/AJAX question

I am using XMLHttpRequest() to create an object and send a request to the server. With my limited knowledge, I understand that the ‘readyState’ goes from 0 to 4, and 4 being the value when the request is completed.

I need to handle a situation where network disconnection (or prolonged delay) can occur. If someone can help me understand the following, I will appreciate it very much.

  1. If there is a network disconnection after the request has been sent, what happens to the ‘readyState’

  2. If the responseText has been partially received and the network gets disconnected, what does the client do with the responseText. That is does it discard the responseText or return the partial responseText to the calling program


I am quite unfamiliar with this. Unfortunately, I do not have whole lot of time to read up this stuff.

What I need to understand is, if there is network disconnection, would the request eventually timeout on its own or I have to set a timer to abort the call?

What would be the value of ‘readyState’ if a request is timed out?

In order for you to check a timeout you will need to set a timer, from the time the timer starts to x amount of seconds/minute that will be the maximum time it has to execute, otherwise the request timed out.


setTimeout(yourTimeoutFunction, 9000); // it has 9 seconds to execute, the timer starts now

//your code...

//Check your readyState ... it may not reach here because the timer fired.

You will have to adjust the time based on your needs.