Browse > Home > Flash CS3/AS3 > 让timer事件不延迟第一次调度

| Subcribe via RSS

让timer事件不延迟第一次调度

四月 23rd, 2008 | 304 views | Posted by flashlizi in Flash CS3/AS3

一般启动Timer对象创建计时器后,都要经过Timer.delay的延迟后才会第一次调度timer事件。不过有些时候,我们想不经过delay就第一次调度timer事件,因此我们可以扩展Timer类来实现:

package {
import flash.utils.Timer;
import flash.events.TimerEvent;
public class MyTimer extends Timer {
private var startDelay:Boolean;
public function MyTimer(delay:Number, repeatCount:int = 0, startDelay:Boolean = true) {
this.startDelay = startDelay;
super(delay, repeatCount);
}
public override function start():void {
if (!startDelay) dispatchEvent(new TimerEvent(TimerEvent.TIMER));
super.start();
}
}
}

使用方法:
//传入startDelay为false即不延迟调度timer事件
var timer:MyTimer = new MyTimer(1000, 5, false);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();

需要注意的是,不延迟第一次调度timer事件不会计入到计时器的总运行次数repeatCount的。即调度完第一次timer事件后,currentCount仍为0。

Leave a Reply