C# 的Timer 在javascript中的实现--基于Typescript

class Timer {

//js 内置的timer对象
private _jsInnerTimerObj: any;
private _enable: boolean;
private _handlerInvoker:any; private _interval: number; get Interval(): number{
return this._interval;
}
set Interval(value: number) {
this._interval = value;
//重新设定新的定时器
if (this._jsInnerTimerObj) {
window.clearInterval(this._jsInnerTimerObj);
} } private _elapsed:any; get Elapsed(): any{
return this._elapsed;
}
set Elapsed(value: any) {
/*if (!isFunction(value)) {
throw new Error("the Elapsed value must be function!");
}*/
this._elapsed = value;
//只有设置了触发事件才定时,没有回调的定时器是无意义的定时执行其中的匿名方法
this._jsInnerTimerObj = window.setInterval(this._handlerInvoker, this.Interval); } /* 构造函数
*@interval 设置定时器的毫秒数
*/
constructor(interval: number) {
this._enable = false;
this.Interval = interval;
var that = this;
this._handlerInvoker = function () {
if (that.Elapsed&&that._enable==true){
that.Elapsed();
}
}
} Start() {
this._enable = true;
}
Stop() {
this._enable = false;
}
} var t = new Timer(500);
t.Elapsed = function () {
document.write(99)
}
t.Start();
上一篇:如何解决Nginx php 50x 错误


下一篇:Python中DataFrame去重