How can i stop ajax request (don't wait untill response come)?

后端 未结 5 928
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-10 18:46

If I use Ajax to send request and this request take long time ..... if I want to send anther request what should I do?

the current behaviour the second request

5条回答
  •  醉话见心
    2021-02-10 18:53

    The XMLHttpRequest object has an abort function. You can use setTimeout to abort a request that is taking too long.

    EDIT: In the case you do not want to use a timer, and a new event occurs that should abort the prior request, then the event handler should do the following

    if(!this.request) return;  // request contains the XMLHttpRequest
    this.request.onreadystatechange = function() {};
    if(this.request.readyState != 4) {
        this.request.abort();
    }
    

    Then after that you can create the new XMLHttpRequest object.

提交回复
热议问题