Version 3, last updated by Kirill Mourzenko at September 03, 2008 13:31 UTC

There are two sorts of timers provided by the Handy ActionScript Toolset inside the com.kirillam.handy.repeaters package, HATTimer and HATFrameTimer which use the system clock and frames respectively. HATTimer class provides a nice wrapper for setInterval so you don't have to deal with it directly. HATFrameTimer allows you to receive enter frame events without using onEnterFrame event handlers. In fact if you'll be using the toolset I would strongly advise you against using onEnterFrame anywhere in your code. If you need it then you should use a HATFrameTimer. It uses a single onEnterFrame undereath the covers and provides a nice looking wrapper for it. They both have the same api:

 // Starts the timer
 start( interval : Number, pulses : Number );

 // Stops the timer
 kill();

 // Tells if the timer is running.
 isActive() : Boolean

The only difference between them is that HATTimer objects take the interval argument as the number of milliseconds to wait between pulsing and HATFrameTimers take the interval as the number of frames. Both parameters are optional. If the interval is null HATFrameTimer will pulse every frame and HATTimer will pulse every 1000 milliseconds. The pulses parameter lets you tell how many times to pulse before being dying. If you leave it out then it'll pulse indefinitely.

The timer is fairly useless unless you actually handle its HATEvent.PULSE event, which will let you know when the timer pulses. Killing the timer stops the timer but doesn't remove the event handler, so you should make sure you do that as well for good practice even if you don't use a strong reference for your handler.

Example:

 import com.kirillam.handy.repeaters.HATFrameTimer;
 import com.kirillam.handy.events.HATEvent;

 var t : HATFrameTimer = new HATFrameTimer();
 t.handle( HATEvent.PULSE, this, onPulse );
 t.start();

 function onPulse( e : HATEvent )
 {
      traceI("PULSE");
 }