如何清除ID未知的Interval? [英] How to clearInterval with unknown ID?

查看:18
本文介绍了如何清除ID未知的Interval?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设某人(邪恶)用 setInterval 为我们设置了一个计时器,但我们不知道它的 ID(我们没有对对象的引用,setInterval 正在返回,也没有它的值)

(function(){setInterval(function(){console.log('pwned')},10000)})();

有没有办法,怎么清除?是否可以通过其他方式访问计时器?或者至少是特定的浏览器/javascript 引擎?

大卫·弗拉纳根 (David Flanagan) 谈到了他的大 JSTDG 的类似话题.setInterval() 方法,在恶意代码中使用 索引中的key指向

<块引用>

... 一些浏览器检测重复的对话框和长时间运行的脚本并给用户阻止他们的选项.但是恶意代码可以使用 setInterval() 等方法来加载 CPU,还可以通过分配大量内存来攻击您的系统.有Web 浏览器没有通用的方法可以防止这种笨手笨脚的攻击.在实际上,这不是 Web 上的常见问题,因为没有人会返回从事这种脚本滥用!

解决方案

从快速测试来看,所有主流浏览器(最新的 Chrome、Firefox 和 IE)都提供非常小的数字作为 ID,因此只需盲目地"遍历所有可能的数字就应该工作正常:

function ClearAllIntervals() {for (var i = 1; i <99999; i++)window.clearInterval(i);}

完整示例:

window.onload = function() {window.setInterval(function() {document.getElementById("Tick").innerHTML += "tick
";}, 1000);window.setInterval(function() {document.getElementById("Tack").innerHTML += "tack
";}, 1000);};函数 ClearAllIntervals() {for (var i = 1; i <99999; i++)window.clearInterval(i);}

#Placeholder div { width: 80px;向左飘浮;}

这将停止所有区间,当然不能在不知道其 ID 的情况下停止特定区间.

您可以自行测试,它应该适用于上述所有主要浏览器.

Say someone (evil) has set us a timer with setInterval, but we don't know its ID (we don't have the reference to the object, that setInterval is returning, nor its value)

(function(){
  setInterval(function(){console.log('pwned')},
              10000)
})();

Is there a way, how to clear it? Is it possible to acces the timer some other way? Or at least in particular browser/javascript engine?

David Flanagan touches similar topic his big JSTDG. setInterval() method, use in malicious code key in the index points to

... Some browsers detect repeated dialog boxes and long-running scripts and give the user the option to stop them. But malicious code can use methods such as setInterval() to load the CPU and can also attack your system by allocating lots of memory. There is no general way that web browsers can prevent this kind of ham-handed attack. In practice, this is not a common problem on the Web since no one returns to a site that engages in this kind of scripting abuse!

解决方案

From quick test, all major browsers (latest Chrome, Firefox and IE) give pretty small numbers as the ID so just looping "blindly" over all possible numbers should work just fine:

function ClearAllIntervals() {
    for (var i = 1; i < 99999; i++)
        window.clearInterval(i);
}

Full example:

window.onload = function() {
    window.setInterval(function() {
        document.getElementById("Tick").innerHTML += "tick<br />";
    }, 1000);
    window.setInterval(function() {
        document.getElementById("Tack").innerHTML += "tack<br />";
    }, 1000);
};

function ClearAllIntervals() {
    for (var i = 1; i < 99999; i++)
        window.clearInterval(i);
}

#Placeholder div { width: 80px; float: left; }

<button type="button" onclick="ClearAllIntervals();">Clear All</button>
<div id="Placeholder">
    <div id="Tick"></div>
    <div id="Tack"></div>
</div>

This will stop all intervals, can't stop specific interval without knowing its ID of course.

As you can test for yourself, it should work on all major browsers mentioned above.

这篇关于如何清除ID未知的Interval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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