8

This question is like another, except that one is asked in the context of JQuery, which I don't use.

Ajax code on my page issues a POST every ten seconds. Once in a while -- every ~600 requests -- my client code hangs waiting for a response that will never show up.

The code will time out and re-issue the Ajax request, but first I want to clean up everything about the outstanding request: I don't want to leave any vestigial state around to muck things up later.

What is the right way to cancel the outstanding request so that things get properly tidied up?

Thanks!

Community
  • 1
  • 1
Pete Wilson
  • 8,198
  • 6
  • 33
  • 50

1 Answers1

5

Store a reference to your last AJAX request and abort it when you start the next one.

For instance:

var lastRequest;

setInterval(function() {
    lastRequest.abort(); // abort the last request

    lastRequest = new XMLHTTPRequest(); // and the rest of your XHR code
}, 10000);
lonesomeday
  • 215,182
  • 48
  • 300
  • 305