代码之家  ›  专栏  ›  技术社区  ›  ground5hark

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

  •  17
  • ground5hark  · 技术社区  · 16 年前

    浏览器是否跟踪活动的 setInterval 和 setTimeout 身份证?或者这完全是由开发者来跟踪的?

    5 回复  |  直到 14 年前
        1
  •  4
  •   s_hewitt    16 年前

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

    摘自w3.org/TR/2009/WD-html5-20090212/no.html(草案,但w3和 http://w3.org/TR/Window

        2
  •  4
  •   Iftah    11 年前

    您可以通过覆盖 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".
    
        3
  •  1
  •   kennebec    16 年前

    如果你想知道计时器是如何被窗口“记住”的,这可能会引起你的兴趣。

    <!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> 
    
        4
  •  0
  •   scunliffe    16 年前

    更新:

    这个问题有两个方面。

    1. 浏览器是否跟踪计时器ID?
    2. 他们可以接近吗

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

    简而言之,是的,他们被跟踪了(正如@s\u休伊特所说,正如 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);
    }    
    

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

        5
  •  0
  •   unigg    16 年前

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