edtimer.as

来自「flash强大事件机制扩展类」· AS 代码 · 共 85 行

AS
85
字号
//------------------------------
import mx.utils.Delegate;
import mx.events.EventDispatcher;
//------------------------------
class AS2.utils.EDTimer extends EventDispatcher
{
	//------------------------------
	private var _timerID:Number;
	private var _delay:Number;
	private var _repeatCount:Number;
	private var _currentCount:Number = 0;
	private var _running:Boolean;
	//------------------------------
	/*
	@parameter	delay:			延迟,单位毫秒.
	@parameter	repeatCount:	重复次数.默认为Infinity(正无穷大);
	*/
	public function EDTimer(delay:Number, repeatCount:Number)
	{
		if (isNaN(delay)) {
			return;
		}
		if (isNaN(repeatCount)) {
			repeatCount = Infinity;
		}
		this._delay = delay;
		this._repeatCount = repeatCount;
	}
	//------------------------------
	public function reset():Void
	{
		this._currentCount = 0;
		this.stop();
	}
	public function start():Void
	{
		this._timerID = setInterval(Delegate.create(this, this.startTimer), this._delay);
		this._running = true;
	}
	public function stop():Void
	{
		clearInterval(this._timerID);
		this._running = false;
	}
	public function toString():String
	{
		return "[EDTimer]";
	}
	//----------------------------------------
	private function startTimer():Void
	{
		this._currentCount++;
		this.dispatchEvent({type:"timer", currentCount:this._currentCount});
		if (this._currentCount == this._repeatCount) {
			this.reset();
			this.dispatchEvent({type:"timerComplete"});
		}
	}
	//------------------------------
	public function get delay():Number
	{
		return this._delay;
	}
	public function set delay(d:Number):Void
	{
		this._delay = d;
	}
	public function get repeatCount():Number
	{
		return this._repeatCount;
	}
	public function set repeatCount(r:Number):Void
	{
		this._repeatCount = r;
	}
	public function get currentCount():Number
	{
		return this._currentCount;
	}
	public function get running():Boolean
	{
		return this._running;
	}
	//------------------------------
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?