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

后端 未结 5 947
爱一瞬间的悲伤
爱一瞬间的悲伤 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 19:12

    It’s sort of a manual process, but you can add a global xhr object and test it on each request. If the readystate is "loading", abort it:

    var xhr;
    var loadUrl = function(url) {
        if ( xhr && xhr.readyState > 0 && xhr.readyState < 4 ) {
            // there is a request in the pipe, abort
            xhr.abort();    
        }
        xhr = $.get(url, function() {
            console.log('success', this);
        });
    };
    
    loadUrl('/ajax/');
    

提交回复
热议问题