Timers lag behind. It seems that the execution time of the event handler of a TimerEvent.TIMER event is added to the delay. Very recently, David Coletta blogged about this issue in this post.
I've created a sample exposing this problem, next to a workaround for the problem (i.e. the SteadyTimer class). View source to download the SteadyTimer class.
Kudos to David Coletta, because I really need a steady timer for Cron4AS (to simulate unix' cron tab behavior in the Flash client).
[UPDATE] Keith Peters: "In fact, after thinking it over, it seems like it might be a necessity. What if you have a timer handler that takes longer than the timer to execute? You'd run into a situation where the next interval was called before the last one was finished, and multiple simultaneous handlers running, which isn't even possible, I think, because AS is not threaded. Forcing all handlers to finish before running them again avoids this situation." Source: http://www.bit-101.com/blog/?p=910


4 Comments
Yup…. 2 years ago.
http://www.bit-101.com/blog/?p=910
Looking at your implementation, I see one issue…
You are creating a new Date object on every tick. Waste of resources and leads to potential garbage collector slowdowns. You can do the same thing with getTimer() which returns the milliseconds since the SWF started running.
Ok thanks, I’ve updated the code with a getTimer() iso a Date object. In general, I try not to use global functions but if it’s better for performance then I’m a fan
.
This does not work for timers that take less than a second or so.
Running at 1/3rd of a second intervals, it loses on average 9.2ms (over a few minute test).
Running at 1/20th of a second, it is basically useless. I’m getting 10ms between some executions and 100 between others.
I haven’t figured out a way around that yet, but I figured maybe you or a person reading this may know.
One Trackback
[...] IDE and compiled it against the Adobe Air 1.5 runtime. Many thanks to Hans Van de Velde for his SteadyTimer class. The SteadyTimer class fixes the lagging problem of the AS3 Timer [...]