中止XMLHttpRequest的所有实例 [英] Abort all instances of XMLHttpRequest

查看:112
本文介绍了中止XMLHttpRequest的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行代码以指定的时间间隔(50毫秒)调用函数SigWebRefresh.

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

tmr = setInterval(SigWebRefresh, 50);

SigWebRefresh执行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);
}

我曾经使用clearInterval清除通过setInterval()方法设置的计时器.

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

 clearInterval(tmr);    

我想中止所有XMLHttpRequest,但xhr2.abort();仅中止该请求的一个实例.如何中止所有未完成的XmlHttpRequest?

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

推荐答案

尝试将每个xhr2变量推入数组,利用Array.prototype.forEach中止存储的每个xhr2变量

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

这篇关于中止XMLHttpRequest的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆