D
D
dollar2018-10-02 10:53:29
JavaScript
dollar, 2018-10-02 10:53:29

How to determine that loading via XMLHttpRequest is not going?

For test #1, I'm trying to load big data. I send a request, after 30 seconds I get a response. Things are good.

The code:
let start = ((new Date()).getTime() / 1000);
function load_String_From_URL(url) {
  let xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    let now = ((new Date()).getTime() / 1000);
    console.log((now-start).toFixed(2),"Stage:", this.readyState, this.status);
    if (this.readyState == 4 && this.status == 200) {
      console.log((now-start).toFixed(2),"Loaded:", url);
      //success
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
}
load_String_From_URL("http://api.antizapret.info/all.php?type=json")

Conclusion:
5bb320540abad581067614.png

And now test number 2 - I send a request and pull out the network cable from the computer (disconnecting from the Internet).
Result:
5bb3239997147831858763.png

An hour passes, two hours, 10 hours. There are no more signals from XMLHttpRequest. No success, no error, the closure just hangs.
How to be? What if it is loaded there at 1 byte per minute? Well, the weak Internet in the village with my grandmother happens. You just have to wait a week. If so, then it is not good to interrupt after 10 hours by timeout.
Is there a reliable way to determine that data is not coming?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
uiworks, 2018-10-02
@dollar

xhr.timeout = 30000; 
xhr.ontimeout = function() {
  //обработка
}

I
Ihor Bratukh, 2018-10-02
@BRAGA96

As an option to check internet connections:window.navigator.onLine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question