Chapter 22. Timers and Time-Driven Programming

FEATURED CLASSES

  • flash.utils.Timer

  • flash.events.TimerEvent

  • flash.utils.getTimer()

  • flash.utils.setTimeout()

  • flash.utils.setInterval()

  • flash.utils.clearInterval()

The Timer class triggers time-based events in ActionScript 3.0. Nearly any application that involves real-world time uses a timer. Timers can be useful for animation, delaying an action, timing out when an asynchronous action takes too long to execute, triggering an action repeatedly or after a delay, or synchronizing several actions.

ActionScript 3.0 also provides access to several old-school timer methods that aren't object-oriented and don't use the event framework but can have their purposes.

In this chapter, I'll discuss how to set up your own timers, and offer a few tricks to help you get the most out of them.

Timer Basics

For working with time, you'll be using the flash.utils.Timer class.

Each instance of a Timer will, while it's running, repeatedly fire events spaced out by a set amount of time. Using events rather than callback functions not only makes Timers more consistent and easier to use, but it allows you to trigger multiple functions with a single Timer.

Timers are similar in some ways to for loops in that both structures can be used to repeat a bit of code several times but are otherwise quite different. for loops execute every iteration without pause and without coming up for air. No other code is executed until the for loop is finished. Long for loops can prevent your ...

Get ActionScript 3.0 Bible 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.