Code Snippet

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

写了个简单Timer类,代替另类的 setInterval 函数

/*
* Timer类
* @author: kmlxk@yahoo.com.cn
* @created: 2011-2-12 18:51
* 方法:
* 构造函数: Timer(间隔, 回调函数)
* 清除: clear()
* 使用示例: var timer = new Timer(200, function(t) {alert('nana'); t.clear();} );
*/
function Timer(interval, functor) {
this.id = 'timer_'+Math.ceil(Math.random()*900000000+100000000);
eval(
this.id+' = this;');
this.tid = setInterval(this.id+'.callback()',interval)
this.functor = functor;
this.callback = function(){
this.functor(this);
}
this.clear = function(){
clearInterval(
this.tid);
}
}
posted on 2011-02-12 18:58  kmlxk  阅读(1671)  评论(0编辑  收藏  举报