javascript 如何清除ID未知的Interval?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6843201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clearInterval with unknown ID?
提问by mykhal
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)
假设有人(邪恶)为我们设置了一个计时器setInterval
,但我们不知道它的 ID(我们没有对对象的引用,setInterval 正在返回,也没有它的值)
(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?
有没有办法,如何清除它?是否可以通过其他方式访问计时器?或者至少特别是浏览器/javascript引擎?
David Flanagan touches similar topic his big JSTDG.
setInterval() method, use in malicious code
key in the index points to
大卫·弗拉纳根 (David Flanagan) 在他的大 JSTDG 中也谈到了类似的话题。
setInterval() method, use in malicious code
索引中的键指向
... 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!
... 一些浏览器检测重复的对话框和长时间运行的脚本,并为用户提供停止它们的选项。但是恶意代码可以使用诸如 setInterval() 之类的方法来加载 CPU,还可以通过分配大量内存来攻击您的系统。Web 浏览器没有通用的方法可以防止这种笨手笨脚的攻击。实际上,这不是 Web 上的常见问题,因为没有人会返回从事此类脚本滥用的站点!
回答by Shadow Wizard is Ear For You
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:
从快速测试来看,所有主要浏览器(最新的 Chrome、Firefox 和 IE)都给出了非常小的数字作为 ID,因此只需“盲目地”遍历所有可能的数字就可以正常工作:
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 allintervals, can't stop specific interval without knowing its ID of course.
这将停止所有间隔,当然不能在不知道其 ID 的情况下停止特定间隔。
As you can test for yourself, it should work on all major browsers mentioned above.
由于您可以自己测试,它应该适用于上述所有主要浏览器。
回答by HBP
Well, empirically trials in Chrome show that setInterval
returns a number which increments for each call. So if you are SURE that you setInterval was the last one set the following would work :
好吧,Chrome 中的经验试验表明,它setInterval
返回一个数字,每次调用都会增加。因此,如果您确定 setInterval 是最后一组,则以下内容将起作用:
function clearLastInterval () {
var i = setInterval (function () {}, 10000);
clearInterval (i-1);
clearInterval (i);
}
I'm not sure I would recommend this though ;-)
不过我不确定我会推荐这个 ;-)
回答by Mark Chackerian
I tried the approach suggested by #Shadow Wizard and it worked in clearing the interval. However, this approach had side effects afterwards. In my particular case, I was unable use jquery.fadeTo() after clearing all of the intervals.
我尝试了#Shadow Wizard 建议的方法,它可以清除间隔。然而,这种方法后来产生了副作用。在我的特殊情况下,清除所有间隔后我无法使用 jquery.fadeTo() 。
The approach that I settled on is a cleaner solution, which is to redefine the setInterval method and save the interval ids in the re-defined methods. As shown here, I put the the ids into an array and then clear all of them. With a little more refinement of the structure to store the arrays, you could label them and clear them selectively.
我选择的方法是一种更简洁的解决方案,即重新定义 setInterval 方法并将间隔 id 保存在重新定义的方法中。如此处所示,我将 id 放入一个数组中,然后清除所有这些 id。通过对存储数组的结构进行更多改进,您可以标记它们并有选择地清除它们。
var intervalTracking = new Array();
var intervalCount=0;
window.oldSetInterval = window.setInterval;
window.setInterval = ( function(func, interval) {
var interval = oldSetInterval(func, interval);
intervalTracking[++intervalCount]=interval;
return interval;
});
function clearAllIntervals() {
for (var i = 0 ; i <= intervalCount ; i++) {
window.clearInterval( intervalTracking[i] );
}
}
This seems to work!
这似乎有效!
回答by Daniel Garcia Sanchez
I solved it by using localstorage, saving there the id of the setInterval, and picking up it later in order to clear that interval..
我通过使用 localstorage 解决了这个问题,在那里保存了 setInterval 的 id,稍后再拿起它以清除该间隔..