javascript - xajax - Anule todas las instancias de XMLHttpRequest
jquery ajax put (3)
Intenta empujar cada variable xhr2 a una matriz, utiliza Array.prototype.forEach para abortar cada variable xhr2 almacenada
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()
})
Tengo esta línea de código que llama a la función SigWebRefresh a intervalos específicos ( 50 milisegundos).
tmr = setInterval(SigWebRefresh, 50);
SigWebRefresh realiza 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);
}
Yo había usado clearInterval que borra un temporizador establecido con el método setInterval ().
clearInterval(tmr);
Quiero abortar todo XMLHttpRequest pero xhr2.abort(); solo cancela una instancia de la solicitud. ¿Cómo abortar todo XmlHttpRequest sin completar?
Podría implementar una lista de todas las solicitudes de procesamiento:
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
}
Y luego aclarar:
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);
}