首页 > 代码库 > flash Timer 性能优化,每几秒间隔一次

flash Timer 性能优化,每几秒间隔一次

timer.stop后timer.currentCount没有重置,timer.reset后,currentCount重置了。
package game.mananger{    import flash.events.TimerEvent;    import flash.utils.Dictionary;    import flash.utils.Timer;        /**     *提供 一个 1秒间隔不断跑的timer,可以注册几秒钟回调,     * 用于提高性能,全局仅有这一个timer      * @author Administrator     *      */        public class GTimerManager extends BaseManager    {        private var _timer:Timer;        private var _funAry:Array;        public function GTimerManager()        {            if(!_timer)            {                _timer = new Timer(1000);                _timer.addEventListener(TimerEvent.TIMER,_interval);                _timer.start();                                _funAry = [];            }        }        private function _interval(evt:TimerEvent):void        {            var len:int = _funAry.length;            for(var i:int = 0;i<len;i++)            {                var info:GFunInfo = _funAry[i];                if(info.isReset)                {                    info.temp ++;                    if(info.temp % info.delay == 0){                        if(info.fun!=null)                        {                            info.fun();                        }                    }                }else if(_timer.currentCount % info.delay == 0)                {                    if(info.fun!=null)                    {                        info.fun();                    }                }            }        }        /**         *注册函数进去          * @param fun         * @param senconds 几秒间隔         */                public function add(struct:GFunInfo):void        {            if(_funAry.indexOf(struct)==-1)            {                _funAry.push(struct);                if(struct.isReset){                    struct.temp = 0;                }            }        }        /**         *移除函数         * @param fun         *          */                public function remove(struct:GFunInfo):void        {            var index:int = _funAry.indexOf(struct);            if(index != -1)            {                _funAry.splice(struct,1);            }        }        /**         *销毁          *          */                public function destory():void        {            _funAry.length = 0;            _timer.stop();            _timer = null;        }    }}

package game.mananger{    public class GFunInfo    {        /**         *          * @param fun 回调函数         * @param delay 间隔秒数         * @isReset isReset 重新add后是否继续上次的计数         */                public function GFunInfo(fun:Function,delay:int,isReset:Boolean)        {            this.fun = fun;            this.delay = delay;            this.isReset = isReset;        }        public var fun:Function;        public var delay:int;        public var isReset:Boolean;        public var temp:int;    }}
        testGtimer();               private function testGtimer():void        {            var g:GTimerManager = new GTimerManager();            g.add(new GFunInfo(testGfun,5,true));            g.add(new GFunInfo(testGfun2,1,false));        }        private function testGfun():void        {            trace("testGfun");        }        private function testGfun2():void        {            trace("testGfun2");        }

 

flash Timer 性能优化,每几秒间隔一次