I tried this flex example from blog.flexexamples.com. This is a basic timer example.
But when timer crosses 30 min mark, it restarts automatically and starts showing negative time value(-1,-2 ..).
What is the problem ? Why it is showing negative value of timer, after it crosses 30 min mark ?
TimerDemo.mxml
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/14/creating-a-simple-timer-in-flex-with-the-flashutilstimer-class/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init()">
<mx:Script>
<![CDATA[
import flash.utils.Timer;
import flash.events.TimerEvent;
private const MIN_MASK:String = "00";
private const SEC_MASK:String = "00";
private const MS_MASK:String = "000";
private const TIMER_INTERVAL:Number = 10;
private var baseTimer:int;
private var t:Timer;
private function init():void {
t = new Timer(TIMER_INTERVAL);
t.addEventListener(TimerEvent.TIMER, updateTimer);
}
private function updateTimer(evt:TimerEvent):void {
var d:Date = new Date(getTimer()-baseTimer);
var min:String = (MIN_MASK + (d.minutes - 30.0)).toString();
var sec:String = (SEC_MASK + d.seconds).toString();
var ms:String = (MS_MASK + d.milliseconds).toString();
counter.text = String(min+":"+sec+"."+ms);
}
private function startTimer():void {
baseTimer = getTimer();
t.start();
}
private function stopTimer():void {
t.stop();
}
]]>
</mx:Script>
<mx:ApplicationControlBar dock="true">
<mx:Button label="Start timer" click="startTimer()" />
<mx:Button label="Stop timer" click="stopTimer()" />
</mx:ApplicationControlBar>
<mx:Label id="counter" fontSize="96" />
</mx:Application>
I’m not really sure what you’re trying to do but I think I know what your problem is.
By subtracting 30 of the
minvar, you only manipulate your output. So the actual timer is still counting the real minutes.If you want to subtract 30 minutes from your Timer you should do it when instantiating
d:Datelike so:Remember to also remove the
-30for yourvar min:StringI hope this is what you’re looking for.
EDIT
Given your comment about your
getTimer()andbaseTimerhaving a lot of difference, you should have a look at the getTimer() references. It states the following:When I trace
baseTimerandgetTimer()within thestartTimer()function they are always (more or less) the same. So it seems there is something off in your case.A workaround can be using
new Date().timeclass instead ofgetTimer()like so:More info on that can be found in the reference