| 包 | flash.utils |
| 类 | public class Timer |
| 继承 | Timer EventDispatcher Object |
| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
start() 方法来启动计时器。为 timer 事件添加事件侦听器,以便将代码设置为按计时器间隔运行。
可以创建 Timer 对象以运行一次或按指定间隔重复运行,从而按计划执行代码。根据 SWF 文件的帧速率或运行时环境(可用内存和其他因素),运行时调度事件的间隔可能稍有不同。例如,如果某个 SWF 文件设置为以每秒 10 帧 (fps)(也就是 100 毫秒的间隔)的速度播放,但计时器设置为在 80 毫秒时触发事件,则将按接近于 100 毫秒的间隔触发事件。大量耗费内存的脚本也可能使事件发生偏差。
| 属性 | 由以下参数定义 | ||
|---|---|---|---|
![]() | constructor : Object
对类对象或给定对象实例的构造函数的引用。 | Object | |
| currentCount : int [只读]
计时器从 0 开始后触发的总次数。 | Timer | ||
| delay : Number
计时器事件间的延迟(以毫秒为单位)。 | Timer | ||
| repeatCount : int
设置的计时器运行总次数。 | Timer | ||
| running : Boolean [只读]
计时器的当前状态;如果计时器正在运行,则为 true,否则为 false。 | Timer | ||
| 方法 | 由以下参数定义 | ||
|---|---|---|---|
使用指定的 delay 和 repeatCount 状态构造新的 Timer 对象。 | Timer | ||
![]() | addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
使用 EventDispatcher 对象注册事件侦听器对象,以使侦听器能够接收事件通知。 | EventDispatcher | |
![]() |
将事件调度到事件流中。 | EventDispatcher | |
![]() |
检查 EventDispatcher 对象是否为特定事件类型注册了任何侦听器。 | EventDispatcher | |
![]() |
表示对象是否已经定义了指定的属性。 | Object | |
![]() |
表示 Object 类的实例是否在指定为参数的对象的原型链中。 | Object | |
![]() |
表示指定的属性是否存在、是否可枚举。 | Object | |
![]() |
从 EventDispatcher 对象中删除侦听器。 | EventDispatcher | |
如果计时器正在运行,则停止计时器,并将 currentCount 属性设回为 0,这类似于秒表的重置按钮。 | Timer | ||
![]() |
设置循环操作动态属性的可用性。 | Object | |
如果计时器尚未运行,则启动计时器。 | Timer | ||
停止计时器。 | Timer | ||
![]() |
返回此对象的字符串表示形式,其格式设置遵守区域设置特定的约定。 | Object | |
![]() |
返回指定对象的字符串表示形式。 | Object | |
![]() |
返回指定对象的原始值。 | Object | |
![]() |
检查是否用此 EventDispatcher 对象或其任何祖代为指定事件类型注册了事件侦听器。 | EventDispatcher | |
| 事件 | 摘要 | 由以下参数定义 | ||
|---|---|---|---|---|
![]() | [广播事件] Flash Player 或 AIR 应用程序获得操作系统焦点并变为活动状态时将调度此事件。 | EventDispatcher | ||
![]() | [广播事件] Flash Player 或 AIR 应用程序失去操作系统焦点并变为非活动状态时将调度此事件。 | EventDispatcher | ||
| 每当 Timer 对象达到根据 Timer.delay 属性指定的间隔时调度。 | Timer | |||
| 每当它完成 Timer.repeatCount 设置的请求数后调度。 | Timer | |||
currentCount | 属性 |
delay | 属性 |
delay:Number| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
计时器事件间的延迟(以毫秒为单位)。如果在计时器正在运行时设置延迟间隔,则计时器将按相同的 repeatCount 迭代重新启动。
注意:建议 delay 不要低于 20 毫秒。计时器频率不得超过 60 帧/秒,这意味着低于 16.6 毫秒的延迟可导致出现运行时问题。
实现
public function get delay():Number public function set delay(value:Number):void引发
Error — 如果指定的延迟为负数或不是有限数,则将引发异常。
|
repeatCount | 属性 |
repeatCount:int| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
设置的计时器运行总次数。如果重复计数设置为 0,则计时器将持续不断运行,最长可运行 24.86 天,或直至调用了 stop() 方法或节目停止。如果重复计数不为 0,则将运行计时器,运行次数为指定的次数。如果设置的 repeatCount 总数等于或小于 currentCount,则计时器将停止并且不会再次触发。
实现
public function get repeatCount():int public function set repeatCount(value:int):voidrunning | 属性 |
Timer | () | 构造函数 |
public function Timer(delay:Number, repeatCount:int = 0)| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
使用指定的 delay 和 repeatCount 状态构造新的 Timer 对象。
计时器不会自动启动;必须调用 start() 方法来启动它。
delay:Number — 计时器事件间的延迟(以毫秒为单位)。建议 delay 不要低于 20 毫秒。计时器频率不得超过 60 帧/秒,这意味着低于 16.6 毫秒的延迟可导致出现运行时问题。
| |
repeatCount:int (default = 0) — 指定重复次数。如果为零,则计时器将持续不断重复运行,最长可运行 24.86 天 (int.MAX_VALUE + 1)。如果不为 0,则将运行计时器,运行次数为指定的次数,然后停止。
|
引发
Error — 如果指定的延迟为负数或不是有限数
|
示例 ( 如何使用本示例 )
创建了一个 Timer 对象,它在 30 秒内启动(将延迟设置为 30000 毫秒),并在总共 90 秒的时间内重复启动 3 次。(计时器将在第 3 次后停止。)
为 myTimer 计时器添加了两个事件侦听器。第一个事件侦听器是由 TimerEvent.TIMER 事件触发的,每次启动计时器时都会发生此事件。timerHandler() 方法将更改 statusTextField 文本字段的文本以反映所剩的秒数。
注意:Timer 类通过增大 currentCount 属性中的数字来跟踪计时器需要启动的次数 (repeats)。
在最后一次调用计时器后,将调度 TimerEvent.TIMER_COMPLETE 事件并调用 completeHandler() 方法。completeHandler() 方法将 inputTextField 文本字段的类型从 INPUT 更改为 DYNAMIC,这意味着用户不再可以输入或更改文本。
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class Timer_constructorExample extends Sprite {
private var statusTextField:TextField = new TextField();
private var inputTextField:TextField = new TextField();
private var delay:uint = 30000;
private var repeat:uint = 3;
private var myTimer:Timer = new Timer(delay, repeat);
public function Timer_constructorExample() {
inputTextField.x = 10;
inputTextField.y = 10;
inputTextField.border = true;
inputTextField.background = true;
inputTextField.height = 200;
inputTextField.width = 200;
inputTextField.multiline = true;
inputTextField.wordWrap = true;
inputTextField.type = TextFieldType.INPUT;
statusTextField.x = 10;
statusTextField.y = 220;
statusTextField.background = true;
statusTextField.autoSize = TextFieldAutoSize.LEFT;
myTimer.start();
statusTextField.text = "You have " + ((delay * repeat) / 1000)
+ " seconds to enter your response.";
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
addChild(inputTextField);
addChild(statusTextField);
}
private function timerHandler(e:TimerEvent):void{
repeat--;
statusTextField.text = ((delay * repeat) / 1000) + " seconds left.";
}
private function completeHandler(e:TimerEvent):void {
statusTextField.text = "Times Up.";
inputTextField.type = TextFieldType.DYNAMIC;
}
}
}
reset | () | 方法 |
public function reset():void| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
如果计时器正在运行,则停止计时器,并将 currentCount 属性设回为 0,这类似于秒表的重置按钮。然后,在调用 start() 后,将运行计时器实例,运行次数为指定的重复次数(由 repeatCount 值设置)。
相关 API 元素
start | () | 方法 |
public function start():void| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
如果计时器尚未运行,则启动计时器。
stop | () | 方法 |
public function stop():void| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
停止计时器。如果在调用 stop() 后调用 start(),则将继续运行计时器实例,运行次数为剩余的 重复次数(由 repeatCount 属性设置)。
相关 API 元素
timer | 事件 |
flash.events.TimerEvent属性 TimerEvent.type =
flash.events.TimerEvent.TIMER| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
每当 Timer 对象达到根据 Timer.delay 属性指定的间隔时调度。
timer 事件对象的 type 属性值。
此事件具有以下属性:
| 属性 | 值 |
|---|---|
bubbles | false |
cancelable | false;没有要取消的默认行为。 |
currentTarget | 当前正在使用某个事件侦听器处理 Event 对象的对象。 |
target | 已达到其间隔的 Timer 对象。 |
timerComplete | 事件 |
flash.events.TimerEvent属性 TimerEvent.type =
flash.events.TimerEvent.TIMER_COMPLETE| 语言版本: | ActionScript 3.0 |
| 运行时版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
每当它完成 Timer.repeatCount 设置的请求数后调度。
timerComplete 事件对象的 type 属性值。
此事件具有以下属性:
| 属性 | 值 |
|---|---|
bubbles | false |
cancelable | false;没有要取消的默认行为。 |
currentTarget | 当前正在使用某个事件侦听器处理 Event 对象的对象。 |
target | 已完成其请求的 Timer 对象。 |
TimerExample 类来说明如何将侦听器方法 timerHandler() 设置为侦听要调度的新 TimerEvent。计时器将在调用 start() 时启动,并且之后将调度计时器事件。
package {
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
public class TimerExample extends Sprite {
public function TimerExample() {
var myTimer:Timer = new Timer(1000, 2);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();
}
public function timerHandler(event:TimerEvent):void {
trace("timerHandler: " + event);
}
}
}
Tue Jun 12 2018, 11:04 AM Z
隐藏继承的公共属性
显示继承的公共属性