浏览器是否跟踪活动计时器 ID?

发布于 2024-09-01 08:50:51 字数 121 浏览 5 评论 0原文

浏览器是否跟踪活动的 setIntervalsetTimeout ID?或者这完全取决于开发人员来跟踪?

如果它确实跟踪它们,是否可以通过 BOM 访问?

Does the browser keep track of active setInterval and setTimeout IDs? Or is this solely up to the developer to keep track of?

If it does keep track of them, is it accessible via the BOM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

总以为 2024-09-08 08:50:51

由开发人员来跟踪。您可以通过使用 setTimeout/setInterval 函数的返回值并将该值传递给clearTimeout/clearInterval 函数来完成此操作 - 如此处其他答案中所述。

这似乎是因为每个浏览器都会以自己的方式实现跟踪间隔。

来自 w3.org/TR/2009/WD-html5-20090212/no.html (草案,但 w3schools 和 http ://w3.org/TR/Window 以几乎相同的方式解释它) - setTimeout 和 setInterval 返回一个 long 值,而clearTimeout/clearInterval 接受一个 long 值来查找和取消

It is up for the developer to keep track of. You can do so by using the returned value of the setTimeout/setInterval function and passing that value to the clearTimeout/clearInterval function - as described in other answers here.

This appears to be because each browser will implement keeping track of the intervals in their own way.

From w3.org/TR/2009/WD-html5-20090212/no.html (a draft, but w3schools and http://w3.org/TR/Window explain it almost the same way) - setTimeout and setInterval return a long and clearTimeout/clearInterval accept a long to find and cancel

靖瑶 2024-09-08 08:50:51

您可以通过覆盖 setTimeout/seInterval 函数来添加此类全局计时器跟踪。作为奖励,您可以在设置或弹出计时器时轻松添加代码,跟踪实时计时器或弹出计时器等...

例如:

timers = {}; // pending timers will be in this variable
originalSetTimeout = window.setTimeout;
// override `setTimeout` with a function that keeps track of all timers
window.setTimeout = function(fu, t) {
    var id = originalSetTimeout(function() {
        console.log(id+" has timed out");
        delete timers[id]; // do not track popped timers 
        fu();
    }, t);
    // track this timer in the `timers` variable
    timers[id] = {id:id,  setAt: new Date(),  timeout: t};
    console.log(id+" has been set to pop in "+t+"ms");
}
// from this point onward all uses of setTimeout will be tracked, logged to console and pending timers will be kept in the global variable "timers".

You can add such global timers tracking by overriding the setTimeout/seInterval functions. As a bonus you easily add code when a timer is set or popped, track live timers or popped timers, etc...

For example:

timers = {}; // pending timers will be in this variable
originalSetTimeout = window.setTimeout;
// override `setTimeout` with a function that keeps track of all timers
window.setTimeout = function(fu, t) {
    var id = originalSetTimeout(function() {
        console.log(id+" has timed out");
        delete timers[id]; // do not track popped timers 
        fu();
    }, t);
    // track this timer in the `timers` variable
    timers[id] = {id:id,  setAt: new Date(),  timeout: t};
    console.log(id+" has been set to pop in "+t+"ms");
}
// from this point onward all uses of setTimeout will be tracked, logged to console and pending timers will be kept in the global variable "timers".
几味少女 2024-09-08 08:50:51

如果您对计时器的窗口如何“记住”计时器感到好奇,您可能会对这一点感兴趣。

<!doctype html> 
<html lang= "en"> 
<head> 
<meta charset= "utf-8"> 
<title>Timer </title> 
</head> 
<body>
<h1>Timers</h1>
<script>

if(!window.timers){
    var timers= [], i= 0;
    while(i<5){
        timers.push(setInterval(function(){
            if(confirm(timers.join('\n')+'\nRemove a timer?')){
                clearInterval(timers.shift());
            }
        },
        i*1000+1000));
        ++i;
    }
}
</script>

</body> 
</html> 

This may interest you, if you are curious about how the timer is 'remembered' by its window.

<!doctype html> 
<html lang= "en"> 
<head> 
<meta charset= "utf-8"> 
<title>Timer </title> 
</head> 
<body>
<h1>Timers</h1>
<script>

if(!window.timers){
    var timers= [], i= 0;
    while(i<5){
        timers.push(setInterval(function(){
            if(confirm(timers.join('\n')+'\nRemove a timer?')){
                clearInterval(timers.shift());
            }
        },
        i*1000+1000));
        ++i;
    }
}
</script>

</body> 
</html> 
傲影 2024-09-08 08:50:51

更新:

这个问题有两个方面。

  1. 浏览器是否跟踪计时器 ID?
  2. 它们是否可访问,

我只能假设#1(以及后来的#2)OP 的意思是一般意义上的“它们是否被跟踪”,因为作为开发人员,他/她希望控制它们。

简而言之,是的,它们会被跟踪(正如 @s_hewitt 所指出的,浏览器将其作为 long 值),并且开发人员可以通过在设置时维护对计时器的引用来管理它们。

作为开发人员,您可以通过调用 (clearInterval(handleRef) 或 clearTimeout(handleRef))

但是没有默认值 window.timers 或类似的集合,为您提供现有计时器的列表 - 如果您觉得需要,您将需要自己维护它。

function startPolling(delay){
  pollHandle = setInterval(doThis, delay);
}
function stopPolling(){
  clearInterval(pollHandle);
}

function doThisIn30minUnlessStopped(){
  timerHandle = setTimeout(doThisThing, 1800000);
}
function stop30minTimer(){
  clearTimeout(timerHandle);
}    

您只需创建一个对计时器的变量引用,并且如果/需要时,按名称清除它。

当您加载另一个页面时,所有计时器都会被浏览器自动清除,因此您不需要维护句柄,并清除它们,除非您需要/想要。

Update:

There are 2 aspects to this question.

  1. Does the browser keep track of timer IDs?
  2. Are they accessible

I can only presume for #1 (and later #2) that the OP means "are they tracked" in the general sense because as a Developer s/he would like control over them.

In short, yes they are tracked (as @s_hewitt noted, as long values by the browser) and they can be managed by the developer by maintaining a reference to the timers when setup.

As a developer you can control (e.g. stop) them by calling (clearInterval(handleRef), or clearTimeout(handleRef))

However there is no default window.timers or similar collection that gives you a list of the existing timers - you will need to maintain that yourself if you feel you need to.

function startPolling(delay){
  pollHandle = setInterval(doThis, delay);
}
function stopPolling(){
  clearInterval(pollHandle);
}

function doThisIn30minUnlessStopped(){
  timerHandle = setTimeout(doThisThing, 1800000);
}
function stop30minTimer(){
  clearTimeout(timerHandle);
}    

You simply need to create a variable reference to your timer, and if/when needed, clear it by name.

When you load another page, all the timers are automatically cleared by the browser so you don't need to maintain a handle, and clear them unless you need/want to.

给不了的爱 2024-09-08 08:50:51

看下面的脚本,浏览器可以记住每个 setTimeout 迭代的 id

for (i = 1; i <= d; i++) {
          (function(j) {
                var delay = j/d; 
               t[j] = setTimeout(function() {      
                      elem.style.top = j+"px";
                     },delay);

            })(i);           
 } 

您可以通过以下方式访问它们

for (i in t) {
      alert(t[i]);  
 }

Look at the scripts below, the browser could remember the id of each setTimeout iteration

for (i = 1; i <= d; i++) {
          (function(j) {
                var delay = j/d; 
               t[j] = setTimeout(function() {      
                      elem.style.top = j+"px";
                     },delay);

            })(i);           
 } 

You can access them by

for (i in t) {
      alert(t[i]);  
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文