Name
getTimer( ) Global Function — determine the age of a movie, in milliseconds
Availability
Flash 4 and later
Synopsis
getTimer( )
Returns
The number of milliseconds that have passed since the movie started playing.
Description
The getTimer( )
function indicates how long a
movie has been playing, in milliseconds. We can use multiple
getTimer( )
checks to govern the timed execution
of a block of code or to add time-based features to a movie, such as
a countdown in a video game. For example, when attached to a movie
clip that contains a text field named
counterOutput
, the following code counts down from
60 to 0:
onClipEvent (load) { // Record the current time var startTime = getTimer( ); // Set the number of seconds to count down var countAmount = 60; // Initialize a variable to keep track of how much time has passed var elapsed = 0; } onClipEvent (enterFrame) { // Check how much time has passed elapsed = getTimer( ) - startTime; // If the time passed is less than the length of our countdown... if (elapsed < countAmount * 1000) { // ...set the text field to show the amount of time left counterOutput = countAmount - Math.floor(elapsed / 1000); } else { // ...otherwise, our countdown is done, so tell the user counterOutput = "Time's UP!"; } }
To determine the number of full seconds that have passed in a movie
(rather than milliseconds), divide the return of getTimer(
)
by 1000 and trim off the decimal portion with either
Math.floor( )
, Math.round(
)
, or Math.ceil( )
. For example:
numSeconds ...
Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.