»
S
I
D
E
B
A
R
«
Don’t User the Timer to Make Timers
February 26th, 2009 by Richard Bates

Weird, huh?  I was recently building an application for AIR that needed a simple timer.  No problem, right?  ActionScript has a nifty little Timer class I can use.  So I built the application around this timer on my Mac, which has a 3.0 Ghz Intel Quad-core.  I tested it, and everything seemed fine.  I wasn’t quite finished, though, and I had to leave on a weekend trip, so I exported the project and put it on my Dell 1545 laptop.  Before I recompiled, I ran it on the laptop.  The clock ticked 4 times as fast.  I was pissed.  And confused.  I had heard that the AS3 Timer is based on framerates, but I didn’t check for myself, and figured it would probably be negligible anyways.  Not the case. Don’t believe me?  Check out this Timer class example from computus.org.  Hint: Try right-clicking the timer, waiting, then clicking outside the timer.  The delay racks up while the movie is paused.

So, I contacted Adobe, and Mike Potter suggested I use the Date class instead.  He didn’t say how, but I deduced that the date must be coming from the system clock rather than some Flash Player internal math.  So, I decided to try a new strategy, creating a StopWatch class that could:

  1. Get the date as a Unix-style timestamp
  2. On each frame, get a new timestamp
  3. Subtract the starting timestamp from the latest timestamp
  4. Parse the result into hours, minutes, and seconds then update a label with it
  5. Repeat

It worked. Brilliantly (thanks Mike!). So, for anyone out there who is facing the same problem, you can check out this sample app and view/download the source.


8 Responses

Damon Edwards writes:
February 26th, 2009 at 9:38 pm

Great article, I use a timer in my application to simulate a “threading” type functionality. I may have to give your timer a shot and see if there are any performance gains. I do notice my timer running suspiciously fast some times.

Again, great article.

Link: Actionscript 3 - Don’t User the Timer to Make Timers | Psyked writes:
February 27th, 2009 at 9:23 am

[...] An interesting bug in the Timer class -http://flexandair.com/?p=32 [...]

Behived writes:
February 27th, 2009 at 11:04 am

and what about the getTimer() function? Is that a reliable way of tracking time?

Richard Bates writes:
February 27th, 2009 at 12:03 pm

Behived,
According to the LiveDocs, the getTimer() function returns the time since the Flash Player was initialized. You certainly could use this number to calculate a stopwatch-like value, but it seemed much more intuitive to me to do it this way. For instance, in my application, I already needed the Unix timestamp, so a class that could easily provide that along with the stopwatch was preferred.

paulmignard » Blog Archive » Actionscript Timer is more of a guideline really… writes:
February 27th, 2009 at 1:41 pm

[...] of Timers in my Flash/Flex code - they are just so darn handy. So imagine my surprise when I read this post that states and shows that Timer tick at an interval based on the swfs framerate which is variable [...]

lee writes:
February 27th, 2009 at 5:15 pm

Framerates have always differed between the Mac, and Windows flashplayer.

Phillip Kerman writes:
February 27th, 2009 at 7:13 pm

I’ve had my share of issues with Timers–but do you have the code in question? In fact, Timers will run at a rate independent of the framerate, but you won’t SEE any update until the next frame… but, you can force it using evt.updateAfterEvent(). Did you try that?

Also, the ultimate solution you describe using date IS locked to the framerate. Was that your concern or that the timer didn’t count correctly? or what?

Iain writes:
February 28th, 2009 at 8:10 pm

Wierd! getTimer() for the win!

Leave a Reply

»  Substance: WordPress   »  Style: Ahren Ahimsa
© 2008-2009 Richard Bates