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

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

问题描述

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

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

有没有办法,如何清除它?是否有可能以其他方式访问计时器?或者至少在特定的浏览器/ javascript引擎中?

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触及类似主题他的大JSTDG。
setInterval()方法,在恶意代码中使用索引中的键指向

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


...有些浏览器会检测重复的对话框和长时间运行的脚本,并为用户提供
停止它们的选项。但是恶意代码可以使用诸如setInterval()之类的方法来加载CPU,并且还可以通过分配大量内存来攻击你的系统。没有一般的方式,网络浏览器可以防止这种火腿式攻击
。在
练习中,这不是Web上的常见问题,因为没有人返回到
从事此类脚本滥用的网站!

... 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!


推荐答案

从快速测试开始,所有主流浏览器(最新的Chrome,Firefox和IE)都提供相当小的数字作为ID,因此只是盲目地循环所有可能的数字应该可以正常工作:

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

完整示例:

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>

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

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天全站免登陆