ActionScript 3.0 Language and Components Reference | All Packages | All Classes | Language Elements | Index | Appendixes | Conventions | Frames No Frames |
Timer

Package | flash.utils
Class | public class Timer
Inheritance | Timer EventDispatcher Object

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

The Timer class is the interface to Flash Player timers. You can create new Timer objects to run code on a specified time sequence. Use the `start()` method to start a timer. Add an event listener for the `timer` event to set up code to be run on the timer interval.

You can create Timer objects to run once or repeat at specified intervals to execute code on a schedule. Depending on the SWF file's framerate or Flash Player's environment (available memory and other factors), Flash Player may dispatch events at slightly offset intervals. For example, if a SWF file is set to play at 10 frames per second [fps], which is 100 millisecond intervals, but your timer is set to fire an event at 80 milliseconds, Flash Player will fire the event close to the 100 millisecond interval. Memory-intensive scripts may also offset the events.

View the examples

See also

Working with dates and times
Controlling time intervals

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | currentCount : int [read-only] The total number of times the timer has fired since it started at zero. | Timer
|  | delay : Number The delay, in milliseconds, between timer events. | Timer
|  | prototype : Object [static] A reference to the prototype object of a class or function object. | Object
|  | repeatCount : int The total number of times the timer is set to run. | Timer
|  | running : Boolean [read-only] The timer's current state; true if the timer is running, otherwise false. | Timer

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | Timer(delay:Number, repeatCount:int = 0) Constructs a new Timer object with the specified delay and repeatCount states. | Timer
|  | addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event. | EventDispatcher
|  | dispatchEvent(event:Event):Boolean Dispatches an event into the event flow. | EventDispatcher
|  | hasEventListener(type:String):Boolean Checks whether the EventDispatcher object has any listeners registered for a specific type of event. | EventDispatcher
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void Removes a listener from the EventDispatcher object. | EventDispatcher
|  | reset():void Stops the timer, if it is running, and sets the currentCount property back to 0, like the reset button of a stopwatch. | Timer
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | start():void Starts the timer, if it is not already running. | Timer
|  | stop():void Stops the timer. | Timer
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object
|  | willTrigger(type:String):Boolean Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type. | EventDispatcher

Events
Hide Inherited Events
Show Inherited Events

| Event | Summary | Defined By
--- | --- | --- | ---
|  | activate | Dispatched when Flash Player gains operating system focus and becomes active. | EventDispatcher
|  | deactivate | Dispatched when Flash Player loses operating system focus and is becoming inactive. | EventDispatcher
|  | timer | Dispatched whenever a Timer object reaches an interval specified according to the Timer.delay property. | Timer
|  | timerComplete | Dispatched whenever it has completed the number of requests set by Timer.repeatCount. | Timer

Property Detail

currentCount | property

`currentCount:int` [read-only]

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

The total number of times the timer has fired since it started at zero. If the timer has been reset, only the fires since the reset are counted.

Implementation
`public function get currentCount():int`

delay | property |

`delay:Number` [read-write]

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

The delay, in milliseconds, between timer events. If you set the delay interval while the timer is running, the timer will restart at the same `repeatCount` iteration.

Implementation
`public function get delay():Number`
`public function set delay(value:Number):void`
Throws

| `Error` — Throws an exception if the delay specified is negative or not a finite number.

repeatCount | property |

`repeatCount:int` [read-write]

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

The total number of times the timer is set to run. If the repeat count is set to 0, the timer continues forever or until the `stop()` method is invoked or the program stops. If the repeat count is nonzero, the timer runs the specified number of times. If `repeatCount` is set to a total that is the same or less then `currentCount` the timer stops and will not fire again.

Implementation
`public function get repeatCount():int`
`public function set repeatCount(value:int):void`

running | property |

`running:Boolean` [read-only]

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

The timer's current state; `true` if the timer is running, otherwise `false`.

Implementation
`public function get running():Boolean`
Constructor Detail

Timer | () | Constructor

`public function Timer(delay:Number, repeatCount:int = 0)`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Constructs a new Timer object with the specified `delay` and `repeatCount` states.

The timer does not start automatically; you must call the `start()` method to start it.

Parameters

| `delay:Number` — The delay between timer events, in milliseconds.
| `repeatCount:int` (default = `0`)`` — Specifies the number of repetitions. If zero, the timer repeats infinitely. If nonzero, the timer runs the specified number of times and then stops.

Throws

| `Error` — if the delay specified is negative or not a finite number

Example
In the following example, the user is given 90 seconds to write a response in an input text field. Also, every 30 seconds, a status message lets the user know how many seconds are left.

A Timer object is created that starts in 30 seconds (delay is set to 30000 milliseconds) and repeats three times, for a total of 90 seconds. (The timer stops after the third time.)

Two event listeners are added for the `myTimer` timer. The first is triggered by the `TimerEvent.TIMER` event, which occurs every time the timer is started. The `timerHandler()` method changes the text for the `statusTextField` text field to reflect the seconds remaining.

Note: The Timer class keeps track of the number of times it has to start (`repeats`) by increasing the number in the `currentCount` property.)

After the timer is called for the last time, the `TimerEvent.TIMER_COMPLETE` event is dispatched and the `completeHandler()` method is called. The `completeHandler()` method changes the type of the `inputTextField` text field from `INPUT` to `DYNAMIC`, which means the user can no longer enter or change text.

    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 write 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;
            }
        }
    }

Method Detail

reset | () | method

`public function reset():void`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Stops the timer, if it is running, and sets the `currentCount` property back to 0, like the reset button of a stopwatch. Then, when `start()` is called, the timer instance runs for the specified number of repetitions, as set by the `repeatCount` value.

See also

Timer.stop()

start | () | method |

`public function start():void`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Starts the timer, if it is not already running.

stop | () | method |

`public function stop():void`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Stops the timer. When `start()` is called after `stop()`, the timer instance runs for the remaining number of repetitions, as set by the `repeatCount` property.

See also

Timer.reset()
Event Detail

timer | Event

Event Object Type: `flash.events.TimerEvent`
TimerEvent.type property = `flash.events.TimerEvent.TIMER`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Dispatched whenever a Timer object reaches an interval specified according to the `Timer.delay` property.

Defines the value of the `type` property of a `timer` event object.

This event has the following properties:

Property | Value
--- | ---
`bubbles` | `false`
`cancelable` | `false`; there is no default behavior to cancel.
`currentTarget` | The object that is actively processing the Event object with an event listener.
`target` | The Timer object that has reached its interval.

timerComplete | Event |

Event Object Type: `flash.events.TimerEvent`
TimerEvent.type property = `flash.events.TimerEvent.TIMER_COMPLETE`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Dispatched whenever it has completed the number of requests set by `Timer.repeatCount`.

Defines the value of the `type` property of a `timerComplete` event object.

This event has the following properties:

Property | Value
--- | ---
`bubbles` | `false`
`cancelable` | `false`; there is no default behavior to cancel.
`currentTarget` | The object that is actively processing the Event object with an event listener.
`target` | The Timer object that has completed its requests.

Examples How to use examples
TimerExample.as
The following example uses the class `TimerExample` to show how a listener method `timerHandler()` can be set to listen for a new TimerEvent to be dispatched. The timer is started when `start()` is called, and after that point, the timer events are dispatched.

    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);
            }
        }
    }

Submit Feedback on LiveDocs
© 2004-2008 Adobe Systems Incorporated. All rights reserved.
Tue May 20 2008, 04:02 AM -07:00