Abort all instances of XMLHttpRequest

橙三吉。 提交于 2019-12-11 11:55:34

问题


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 ?


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/32900103/abort-all-instances-of-xmlhttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!