开发者

How to correctly cancel an outstanding Ajax request that will never complete?

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 neve开发者_开发问答r 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!


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜