author suo 去除无用方法 整理ts与as3语法差异 (代替原生Timer,setInterval)
  class Timer { private private static _pool : Array < any > = [ ] ; /** 两帧之间的时间间隔,单位毫秒。*/ private _delta : number = 0 ; /** 时针缩放。*/ public scale : number = 1 ; /** 当前帧开始的时间。*/ public currTimer : number = this . _now ( ) ; /** 当前的帧数。*/ public currFrame : number = 0 ; private private _lastTimer : number = this . _now ( ) ; private private _mid : number = 1 ; private private _methodMap : Dictionary = new Dictionary ( ) ; private private _laters : Array < any > = [ ] ; private private _handlers : Array < any > = [ ] ; private private _temp : Array < any > = [ ] ; private private _count : number = 0 ; /**
     * 创建 <code>Timer</code> 类的一个实例。
     */ public constructor ( ) { this . _init ( ) ; } /**
     *两帧之间的时间间隔,单位毫秒。
     */ public get delta ( ) : number { return this . _delta ; } private protected _init ( ) { FishingJoy . Laya . timer && FishingJoy . Laya . timer . frameLoop ( 1 , this , this . update ) ; } private protected _now ( ) { return Date . now ( ) ; } private public update ( ) { if ( this . scale <= 0 ) { this . _lastTimer = this . _now ( ) ; return ; } var frame = this . currFrame = this . currFrame + this . scale ; var now = this . _now ( ) ; this . _delta = ( now - this . _lastTimer ) * this . scale ; var timer = this . currTimer = this . currTimer + this . _delta ; this . _lastTimer = now ; var handlers = this . _handlers ; this . _count = 0 ; for ( i = 0 , n = handlers . length ; i < n ; i ++ ) { handler = handlers [ i ] ; if ( handler . method !== null ) { var t = handler . userFrame ? frame : timer ; if ( t >= handler . exeTime ) { if ( handler . repeat ) { if ( ! handler . jumpFrame ) { handler . exeTime += handler . delay ; handler . run ( false ) ; if ( t > handler . exeTime ) { handler . exeTime += Math . ceil ( ( t - handler . exeTime ) / handler . delay ) * handler . delay ; } } else { while ( t >= handler . exeTime ) { handler . exeTime += handler . delay ; handler . run ( false ) ; } } } else { handler . run ( true ) ; } } } else { this . _count ++ ; } } if ( this . _count > 30 || frame % 200 === 0 ) this . _clearHandlers ( ) ; var laters = this . _laters ; for ( var i = 0 , n = laters . length - 1 ; i <= n ; i ++ ) { var handler = laters [ i ] ; if ( handler . method !== null ) { this . _methodMap . remove ( handler . method ) ; handler . run ( false ) ; } this . _recoverHandler ( handler ) ; i === n && ( n = laters . length - 1 ) ; } laters . length = 0 ; } private private _clearHandlers ( ) { var handlers = this . _handlers ; for ( var i = 0 , n = handlers . length ; i < n ; i ++ ) { var handler = handlers [ i ] ; if ( handler . method !== null ) this . _temp . push ( handler ) ; else this . _recoverHandler ( handler ) ; } this . _handlers = this . _temp ; this . _temp = handlers ; this . _temp . length = 0 ; } private private _recoverHandler ( handler ) { if ( this . _methodMap . get ( handler . method ) == handler ) this . _methodMap . remove ( handler . method ) ; handler . clear ( ) ; Timer . _pool . push ( handler ) ; } private public _create ( useFrame , repeat , delay , caller , method , args , coverBefore ) { if ( ! delay ) { method . apply ( caller , args ) ; return null ; } if ( coverBefore ) { var handler = this . _getHandler ( caller , method ) ; if ( handler ) { handler . repeat = repeat ; handler . userFrame = useFrame ; handler . delay = delay ; handler . caller = caller ; handler . method = method ; handler . args = args ; handler . exeTime = delay + ( useFrame ? this . currFrame : this . currTimer + this . _now ( ) - this . _lastTimer ) ; return handler ; } } handler = Timer . _pool . length > 0 ? Timer . _pool . pop ( ) : new TimerHandler ( ) ; handler . repeat = repeat ; handler . userFrame = useFrame ; handler . delay = delay ; handler . caller = caller ; handler . method = method ; handler . args = args ; handler . exeTime = delay + ( useFrame ? this . currFrame : this . currTimer + this . _now ( ) - this . _lastTimer ) + 1 ; this . _indexHandler ( handler ) ; this . _handlers . push ( handler ) ; return handler ; } private private _indexHandler ( handler ) { this . _methodMap . set ( handler . method , handler ) ; return ; } param delay param caller param method param args param coverBefore public once ( delay , caller , method , args = null , coverBefore = true ) { this . _create ( false , false , delay , caller , method , args , coverBefore ) ; } param delay param caller param method param args param coverBefore param jumpFrame public loop ( delay , caller , method , args = null , coverBefore = true , jumpFrame = false ) { var handler = this . _create ( false , true , delay , caller , method , args , coverBefore ) ; if ( handler ) handler . jumpFrame = jumpFrame ; } param delay param caller param method param args param coverBefore public frameOnce ( delay , caller , method , args = null , coverBefore = true ) { this . _create ( true , false , delay , caller , method , args , coverBefore ) ; } param delay param caller param method param args param coverBefore public frameLoop ( delay , caller , method , args = null , coverBefore = true ) { this . _create ( true , true , delay , caller , method , args , coverBefore ) ; } /** 返回统计信息。*/ public toString ( ) { return "callLater:" + this . _laters . length + " handlers:" + this . _handlers . length + " pool:" + Timer . _pool . length ; } param caller param method public clear ( caller , method ) { var handler = this . _getHandler ( caller , method ) ; if ( handler ) { this . _methodMap . remove ( handler . method ) ; handler . clear ( ) ; } } param caller public clearAll ( caller ) { if ( ! caller ) return ; for ( var i = 0 , n = this . _handlers . length ; i < n ; i ++ ) { var handler = this . _handlers [ i ] ; if ( handler . caller === caller ) { this . _methodMap . remove ( handler . method ) ; handler . clear ( ) ; } } } private private _getHandler ( caller , method ) { for ( var i = 0 , n = this . _handlers . length ; i < n ; i ++ ) { var handler = this . _handlers [ i ] ; if ( handler . caller === caller && handler . method == method ) { return handler ; } } return null ; } param caller param method param args public callLater ( caller , method , args = null ) { if ( this . _getHandler ( caller , method ) == null ) { if ( Timer . _pool . length ) var handler = Timer . _pool . pop ( ) ; else handler = new TimerHandler ( ) ; handler . caller = caller ; handler . method = method ; handler . args = args ; this . _indexHandler ( handler ) ; this . _laters . push ( handler ) ; } } param caller param method public runCallLater ( caller , method ) { var handler = this . _getHandler ( caller , method ) ; if ( handler && handler . method != null ) { this . _methodMap . remove ( handler . method ) ; handler . run ( true ) ; } } param caller param method public runTimer ( caller , method ) { this . runCallLater ( caller , method ) ; } } private class TimerHandler { public key : number ; public repeat : Boolean ; public delay : number ; public userFrame : Boolean ; public exeTime : number ; public caller : any ; public method : Function ; public args : Array < any > ; public jumpFrame : Boolean ; public clear ( ) { this . caller = null ; this . method = null ; this . args = null ; } public run ( withClear ) { var caller = this . caller ; if ( caller && caller . destroyed ) return this . clear ( ) ; var method = this . method ; var args = this . args ; withClear && this . clear ( ) ; if ( method == null ) return ; args ? method . apply ( caller , args ) : method . call ( caller ) ; } }