javascript centralized timers

本文讨论了集中式计时器相较于非集中式计时器的优势,并提供了JavaScript集中式计时器的框架实现。重点阐述了仅有一个计时器在页面上同时运行、暂停与恢复计时器以及删除回调函数的过程简化。特别提醒增加并发计时器可能增加浏览器垃圾回收的可能性,通常在浏览器清理未使用变量和对象时发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a centralized timers has benefit over non-centralized timers. the benefit including .

 

1. have one timers object that excute per page at a time. 

2. you can start, stop, resume the timer 

3. process of removing the timer callback is trivilized 

 

 

and one special note about the javascipt garbage collection concering about timers. Increasing the number of simutaneous timer is likely to increase the likelihood of a garbage collection in the browser. this is roughly when the browser goes through and tries to tie up loose ends (removing unused variables, objects, etc...)

 

below show the framework code that has the centralized timer. 

 

 

/**************************************
*@Summary
*  this is the central timer declaration which you can cetralized manage all timer object
*
*  the benifit of the centralized timer object is that 1. only one timer running per page at a time. 2. pause and resume 3. process of removing callbacks is trivilized
*
* special note on that increasing number of simutaneous timers is likely to increase the likelihood of a garbge collection in the browser. Roughly speaking this is when the browser
* goes through and tries to tie up any loose ends (removing unused variables, objects, etc.).
* @Usage:
*   


* @TODO:
* test it.  
***************************************/

var timers = {

  timerID: 0,
  timers: [],
  start: function () {
    if (this.timerID) {
      return;
    }
    (function () {
      for (var i = 0; i < timers.timers.length; i++) {
        if (timers.timers[i]() === false) {
          timers.timers.splice(i, 1);
        }
      }
      // the return value of setTimeout is the timerId
      timers.timerID = setTimeout(argument.callee, 0);
    })();
  },
  stop: function () {
    clearTimeout(this.timerID);
    // you have to explicitly set the stored timeId value to 0
    this.timerID = 0;
  },
  add: function () {
    this.timers.push(fn);
    this.start();
  }
};
 

 

the test code is yet to come. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值