Monday, December 21, 2009

Count Down Timer Flex - Timer()

public var myTimer : Timer = new Timer(1000);
private function reservationTimer():void
{
myTimer.removeEventListener(TimerEvent.TIMER, timerCount);
timerString = "10:00 min";
timerStart = 600000;//10 min = 10x60x1000
myTimer.addEventListener(TimerEvent.TIMER, timerCount);
myTimer.start();
}
private function timerCount(event:TimerEvent):void
{
timerString = getCountDown(timerStart , 1 );
if(timerStart<=0){
myTimer.removeEventListener(TimerEvent.TIMER, timerCount);

}
}
protected function getCountDown(startTime:Number,secondOffset:Number):String
{
var returnString :String = new String();
//defined the time
var e_s:Number=new Number(1000);
var e_m:Number=new Number(60 * 1000);
var e_h:Number=new Number(60 * 60 * 1000);
var e_d:Number=new Number(24 * 60 * 60 * 1000);

var c_s:Number=new Number(0);
var c_m:Number=new Number(0);
var c_h:Number=new Number(0);
var c_d:Number=new Number(0);

var s_s:String=new String("");
var s_m:String=new String("");
var s_h:String=new String("");
var s_d:String=new String("");

if ((startTime / e_d) >= 1)
{
c_d=Math.floor((startTime / e_d));
s_d=String(c_d) + " day" + ((c_d > 1) ? "s" : "");
startTime -= (c_d * e_d);
}
if ((startTime / e_h) >= 1)
{
c_h=Math.floor((startTime / e_h));
s_h=String(c_h) + " hr" + ((c_h > 1) ? "s" : "");
startTime -= (c_h * e_h);
}
if ((startTime / e_m) >= 1)
{
c_m=Math.floor((startTime / e_m));
s_m=String(c_m) + " min" + ((c_m > 1) ? "s" : "");
startTime -= (c_m * e_m);
}
if ((startTime / e_s) >= 1)
{
c_s=Math.floor((startTime / e_s));
s_s=String(c_s) + " sec" + ((c_s > 1) ? "s" : "");
startTime -= (c_s * e_s);
}

returnString = ((s_d != "") ? s_d + " " : "") + ((s_h != "") ? s_h + " " : "") + ((s_m != "") ? s_m + " " : "") + ((s_s != "") ? s_s : "");

timerStart -= 1000*secondOffset;

return returnString;
}

No comments:

Post a Comment

Your comments is appreciate.