<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="creationCompleteHandler()"
viewSourceURL="srcview/index.html">
<mx:HBox x="15" y="15">
<mx:VBox>
<mx:Label text="Normal timer" />
<mx:List width="150" dataProvider="{normalTimerLog}" />
<mx:Label text="{creepingNormalDiff}" />
<mx:Label text="{behindSchedule}" />
</mx:VBox>
<mx:VBox>
<mx:Label text="Steady timer" />
<mx:List width="150" dataProvider="{steadyTimerLog}" />
<mx:Label text="{steadyDiff}" />
</mx:VBox>
</mx:HBox>
<mx:Script>
<![CDATA[
import mx.logging.Log;
import mx.formatters.DateFormatter;
import mx.collections.ArrayCollection;
import be.novio.utils.SteadyTimer;
private var normalTimer:Timer;
private var steadyTimer:SteadyTimer;
private var delay:int = 1000;
private var normalCount:int;
private var steadyCount:int;
[Bindable] private var normalTimerLog:ArrayCollection;
[Bindable] private var steadyTimerLog:ArrayCollection;
[Bindable] private var creepingNormalDiff:String;
[Bindable] private var steadyDiff:String;
[Bindable] private var behindSchedule:String;
private var startDate:Date;
private function creationCompleteHandler():void
{
startDate = new Date();
normalCount = steadyCount = 0;
normalTimerLog = new ArrayCollection();
steadyTimerLog = new ArrayCollection();
normalTimer = new Timer(delay);
steadyTimer = new SteadyTimer(delay);
normalTimer.addEventListener(TimerEvent.TIMER, normalTimerHandler);
steadyTimer.addTimerEventListener(steadyTimerHandler);
normalTimer.start();
steadyTimer.start();
}
private function doSomethingHeavy():void
{
for(var i:int = 0; i<123; i++)
{
var sillyThing:Number = Math.random()*i;
}
}
private function normalTimerHandler(tEvent:TimerEvent):void
{
var now:Date = new Date();
normalCount++;
var creepingTime:Number = normalCount*delay;
var realTime:Number = now.time - startDate.time;
var expectedCount:int = Math.floor(realTime / delay);
doSomethingHeavy();
creepingNormalDiff = "Difference: "+(realTime-creepingTime);
behindSchedule = "# Behind: "+(expectedCount-normalCount);
normalTimerLog.addItemAt({label:new Date().getTime()/1000}, 0);
}
private function steadyTimerHandler(tEvent:TimerEvent):void
{
var now:Date = new Date();
steadyCount++;
var steadyTime:Number = steadyCount * delay;
var realTime:Number = now.time - startDate.time;
doSomethingHeavy();
steadyDiff = "Difference: "+(realTime-steadyTime);
steadyTimerLog.addItemAt({label:new Date().getTime()/1000}, 0);
}
]]>
</mx:Script>
</mx:Application>