cftimer.as

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

AS
88
字号
//----------------------------------------
import mx.utils.Delegate;
//----------------------------------------
class AS2.utils.CFTimer
{
	//----------------------------------------
	private var _timerID:Number;
	private var _delay:Number;
	private var _repeatCount:Number;
	private var _currentCount:Number = 0;
	private var _running:Boolean;
	//----------------------------------------
	public var timer:Function;
	public var timerComplete:Function;
	//----------------------------------------
	/*
	@parameter	delay:			延迟,单位毫秒.
	@parameter	repeatCount:	重复的次数.默认值为Infinity(正无穷大);
	*/
	public function CFTimer(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 "[CFTimer]";
	}
	//----------------------------------------
	private function startTimer():Void
	{
		this._currentCount++;
		this.timer(this._currentCount);
		if (this._currentCount == this._repeatCount) {
			this.reset();
			this.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 + -
显示快捷键?