Aborting the xmlhttprequest

前端 未结 4 910
臣服心动
臣服心动 2020-12-09 17:20

I am using HTML5 for uploading files. I have a button click event attached to the function uploadFile(). It works fine. I also have a separate button to cancel

相关标签:
4条回答
  • 2020-12-09 18:00

    addEventListener will set the context (this) of uploadCanceled to xhr:

    function uploadCanceled(evt) {
        console.log("Cancelled: " + this.status);
    }
    

    Example: http://jsfiddle.net/wJt8A/


    If, instead, you need to trigger xhr.abort through a "Cancel" click, you can return a reference and add any listeners you need after that:

    function uploadFile() {
        /* snip */
        xhr.send(fd);
    
        return xhr;
    }
    
    document.getElementById('submit').addEventListener('click', function () {
        var xhr = uploadFile(),
            submit = this,
            cancel = document.getElementById('cancel');
    
        function detach() {
            // remove listeners after they become irrelevant
            submit.removeEventListener('click', canceling, false);
            cancel.removeEventListener('click', canceling, false);
        }
    
        function canceling() {
            detach();
            xhr.abort();
        }
    
        // detach handlers if XHR finishes first
        xhr.addEventListener('load', detach, false);
    
        // cancel if "Submit" is clicked again before XHR finishes
        submit.addEventListener('click', canceling, false);
    
        // and, of course, cancel if "Cancel" is clicked
        cancel.addEventListener('click', canceling, false);
    }, false);
    

    Example: http://jsfiddle.net/rC63r/1/

    0 讨论(0)
  • 2020-12-09 18:01

    You should be able to reference the "this" keyword in your canceledUpload event handler. That refers to the XMLHttpRequest. Put this in the handler:

    this.abort();
    
    0 讨论(0)
  • 2020-12-09 18:11

    Abort all XMLHttpRequest:

    var array_xhr = new Array();
    function uploadFile(){ 
        ...
        var xhr = new XMLHttpRequest(); 
        array_xhr.push(xhr);
        ...
    }
    
    function abort_all_xhr(){
      if (array_xhr.length>0) {
            for(var i=0; i<array_xhr.length; i++){
                array_xhr[i].abort();
            }
            array_xhr.length = 0;
        };
    }
    
    abort_all_xhr();
    
    0 讨论(0)
  • 2020-12-09 18:16

    You can use AbortController and fetch to abort requests.

    Demo

    Code

    const btn = document.getElementById('btn');
    const btn2 = document.getElementById('btn2');
    const url = 'https://www.mocky.io/v2/5185415ba171ea3a00704eed?mocky-delay=1000ms';
    
    const ctrl;
    
    btn.addEventListener('click', e => {
      ctrl = new AbortController();
      const signal = ctrl.signal;
    
      fetch(url, { signal })
        .then(r => r.json())
        .then(r => {
          console.log('Request result', r);
        }).catch(e => {
          console.error('Request was aborted. Error:', e);
        })
    });
    
    btn2.addEventListener('click', e => {
      ctrl.abort();
    });
    
    0 讨论(0)
提交回复
热议问题