简体   繁体   中英

Abort all instances of XMLHttpRequest

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 ?

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()
})

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM