Abort all instances of XMLHttpRequest

2019-08-10 04:46发布

I have this line of code that calls the function SigWebRefresh at specified intervals (50 milliseconds).

tmr = setInterval(SigWebRefresh, 50);

SigWebRefresh performs XMLHTTPRequest:

function SigWebRefresh(){   
    xhr2 = new XMLHttpRequest();    
    xhr2.open("GET", baseUri + "SigImage/0", true );
    xhr2.responseType = "blob"; 
    xhr2.onload = function (){
        var img = new Image();      
        img.src = getBlobURL(xhr2.response);        
        img.onload = function (){           
           Ctx.drawImage(img, 0, 0);
           revokeBlobURL( img.src );
           img = null;
        }
    }   
    xhr2.send(null);
}

I had use clearInterval that clears a timer set with the setInterval() method.

 clearInterval(tmr);    

I want to abort all XMLHttpRequest but xhr2.abort(); only aborts one instance of the request. How to abort all uncompleted XmlHttpRequest ?

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-10 05:30

Try pushing each xhr2 variable to an array , utilize Array.prototype.forEach to abort each xhr2 variable stored

var requests = [];

function SigWebRefresh(){   
    xhr2 = new XMLHttpRequest();
    requests.push(xhr2);    
    xhr2.open("GET", baseUri + "SigImage/0", true );
    xhr2.responseType = "blob"; 
    xhr2.onload = function (){
        var img = new Image();      
        img.src = getBlobURL(xhr2.response);        
        img.onload = function (){           
           Ctx.drawImage(img, 0, 0);
           revokeBlobURL( img.src );
           img = null;
        }
    }   
    xhr2.send(null);
}

// abort all requests
requests.forEach(function(request) {
  request.abort()
})
查看更多
做自己的国王
3楼-- · 2019-08-10 05:30
 var xhr2 = null;

 function SigWebRefresh(){  
      if( xhr2 != null ) {
            xhr2.abort();
            xhr2 = null;
      }
      xhr2 = new XMLHttpRequest();    
      xhr2.open("GET", baseUri + "SigImage/0", true );
      xhr2.responseType = "blob"; 
      xhr2.onload = function (){
        var img = new Image();      
        img.src = getBlobURL(xhr2.response);        
        img.onload = function (){           
           Ctx.drawImage(img, 0, 0);
           revokeBlobURL( img.src );
           img = null;
        }
     }   
     xhr2.send(null);
 }
查看更多
放荡不羁爱自由
4楼-- · 2019-08-10 05:32

You could implement a list of all the processing requests:

function remove(array, element){
    var index = array.indexOf(element);
    if (index > -1) {
        array.splice(index, 1);
    }
}

var all_requests = [] // The list of requests that are processing
function SigWebRefresh(){   
    var xhr2 = new XMLHttpRequest();    
    xhr2.open("GET", baseUri + "SigImage/0", true );
    xhr2.responseType = "blob"; 
    xhr2.onload = function (){
        var img = new Image();      
        img.src = getBlobURL(xhr2.response);        
        img.onload = function (){           
           Ctx.drawImage(img, 0, 0);
           revokeBlobURL( img.src );
           img = null;
           remove(all_requests, xhr2); // Make sure to remove already finished requests from your list
        }
    }   
    xhr2.send(null);
    all_requests.push(xhr2); // Add processing request to the list
}

And then to clear:

for(var i in all_requests)
    all_requests[i].abort();
all_requests = [] // Clear the list of requests
查看更多
登录 后发表回答