Name
LzTimerClass — Handles calling of a delegate after a specified number of milliseconds.
Description
LzTimer is used to invoke functionality after a specific amount of time. By using a LzDelegate to refer to a particular method, and then by adding a timer to that delegate, the developer can control the timing of the
method's start. Use LzTimer to tweak sequence and timing of visual animations, delay the effects of user events, or implement
user experiences with expiration features.
It should be noted that the time passed to the LzTimer service describes the time before which the delegate may not run -- it is not guaranteed that the delegate will run at that time. Depending on application
needs and client performance, the specified delegate may be invoked at any time after the amount of time specified.
The following example displays some text when a button is clicked, and uses LzTimer to fade the text away after three seconds.
Example 16. Using LzTimer to invoke method
<canvas height="50" >
<button text="Show then Fade" onclick="canvas.showText()" />
<text name="myText" opacity="0.0" bgcolor="#CCCCCC">The Invisible Man</text>
<simplelayout axis="y" spacing="10" />
<method name="showText">
this.myText.setAttribute( "opacity", 1.0 );
this.fadeDelegate = new LzDelegate( this, "fadeText" );
LzTimer.addTimer( this.fadeDelegate, 3000 );
</method>
<method name="fadeText">
this.myText.animate( "opacity", 0.0, 1000 );
</method>
</canvas>
Development Note
The timer calls a given delegate in a specified number of miliseconds.Unfortunately the original semantics of LzTimer allows
one to add more
than one timer to a single delegate, but only allows one to remove the
oldest timer from a delegate -- there's no way of specifying which one.
The logic below is faithful to the original while still attempting to be
efficient. In the common case of one timer per delegate, we simply
store the timer id in the delegate's timer list entry. If the program
attempts to store more than one timer with a single delegate, we shift
to storing an array of them in the timer list entry. This saves an
array allocation in the common single-timer case, at the cost of increased
code complexity.
Superclass Chain
LzTimerClass
Details
Properties (1)
-
timerList
-
private var timerList = { ... };
Methods (5)
-
addTimer()
-
public function addTimer(d : LzDelegate, milisecs : Number);
Adds a timer. NB: The timer guarantees that the delegate will not be called
before the number of miliseconds specified here, but cannot guarantee that
it will be called at exactly that time.
-
countTimers()
-
public function countTimers(d);
-
removeTimer()
-
public function removeTimer(d : LzDelegate);
Removes the first timer that calls the given delegate from the timerlist.
-
removeTimerWithID()
-
private function removeTimerWithID(d : LzDelegate, id);
Removes the timer with the given id that calls the given delegate from the
timerlist.
-
resetTimer()
-
public function resetTimer(d : LzDelegate, milisecs);
Resets the timer for the given delegate to the new amount of time. If the
delegate is not found, a new timer is created.
JavaScript Synopsis
public
LzTimerClass {
prototype public function
addTimer
(
d : LzDelegate,
milisecs : Number);
prototype public function
removeTimer
(
d : LzDelegate);
prototype public function
resetTimer
(
d : LzDelegate,
milisecs);
}