Interruption of an asynchronous request

9

What happens when the user, for example, reloads the page with an asynchronous request in progress? Does the server continue to run the script? And how can I interrupt a request in progress via JS?

    
asked by anonymous 26.06.2014 / 21:07

1 answer

10
  

What happens when the user, for example, reloads the page with an asynchronous request in progress?

It depends on the moment. If the request has already been sent to the server, it is processed normally (but, of course, the response will never be delivered to the client). If the request has not yet left, it never reaches the server and is not processed. In general you will fall into the first case, I do not know under what circumstances you could fire a request and reload the page before it even gets sent.

  

And how can I stop a request in progress via JS?

Considering that you are using XMLHttpRequest , there is a abort method:

// Enviar:
var requisicao = new XMLHttpRequest();
requisicao.open("get", "arquivo.html", true);
requisicao.send();
// CANCELAR:
requisicao.abort();
    
26.06.2014 / 21:13