用timer实现的定时任务工具类,有registerDelay和register俩个方法,一个是延迟任务,一个是立即执行的方法。该定时器依赖于flash的Enterframe帧频,请注意。使用前请调用init方法初始化。如有不足请指出。
package com.core.timer { import flash.display.Sprite; import flash.display.Stage; import flash.events.Event; import flash.utils.Dictionary; import flash.utils.getTimer; /** * 定时器 * @author yuebinbin * @date 2012-12-30 * */ public class EnterframeTimer extends Sprite { /** 注册的任务集合 */ private var _dic:Dictionary; /** 任务数量 */ private var _count:int; private static var _instance:EnterframeTimer; public function EnterframeTimer() { if (_instance != null) { throw new Error(); } } public static function getInstance():EnterframeTimer { if (_instance == null) { _instance = new EnterframeTimer(); } return _instance; } /** * 初始化 * */ public function init(stage:Stage):void { _dic = new Dictionary(); _count = 0; stage.addEventListener(Event.ENTER_FRAME, execute); } private function execute(e:Event):void { var timer:int = getTimer(); for each(var obj:Object in _dic) { if (obj["delayTime"] == 0 && obj["flag"]) { obj["fun"](obj["args"]); obj["times"] = obj["times"] - 1; obj["flag"] = false; } else { if (timer - obj["registerTime"] <= obj["delayTime"] && obj["flag"]) { continue; } if (timer - obj["registerTime"] >= obj["interval"]) { obj["registerTime"] = timer; obj["fun"](obj["args"]); obj["times"] = obj["times"] - 1; obj["flag"] = false; } } if (obj["times"] == 0) { delete _dic[obj["key"]]; } } } /** * 注册任务 * @para interval:间隔时间, times:执行次数, fun:执行函数 * */ public function register(interval:int = 1000, times:int = 1, fun:Function = null, ...args):int { var key:int = -1; if (fun != null) { var _obj:Object = new Object(); key = ++_count; trace(_obj); trace(_obj["fun"]); _obj["registerTime"] = getTimer(); _obj["fun"] = fun; _obj["interval"] = interval; _obj["times"] = times; _obj["args"] = args; _obj["delayTime"] = 0; _obj["flag"] = true; _obj["key"] = key; _dic[key] = _obj; } return key; } /** * 注册延迟任务 * @para interval:间隔时间, times:执行次数, delayTime:延迟时间, fun:执行函数 * */ public function registerDelay(interval:int = 1000, times:int = 1, delayTime:int = 0, fun:Function = null, ...args):int { var key:int = -1; if (fun != null) { var _obj:Object = new Object(); key = ++_count; _obj["registerTime"] = getTimer(); _obj["fun"] = fun; _obj["interval"] = interval; _obj["times"] = times; _obj["args"] = args; _obj["delayTime"] = delayTime; _obj["flag"] = true; _obj["key"] = key; _dic[key] = _obj; } return key; } /** * 移除任务 * */ public function unregister(key:int):void { if (key != -1) { delete _dic[key]; } } /** * 销毁计时器 * */ public function destroy():void { _dic = null; this.removeEventListener(Event.ENTER_FRAME, execute); } } }
测试代码:
package { import com.core.timer.EnterframeTimer; import flash.display.Sprite; import flash.utils.getTimer; public class test extends Sprite { public function test() { EnterframeTimer.getInstance().init(this.stage); EnterframeTimer.getInstance().register(2000,3,function():void{ trace("#####################" + getTimer()); }); } } }
代码输出结果: