[Flex] flex计时器的使用(timer)
用timer可以达到很多功能,定时调用函数,模拟时钟走等等。
定时调用函数:
var myTimer:Timer = new Timer(50, 1); //设定50毫秒,每次走一毫秒
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, handle); //到达计时器终点时调用这个函数
myTimer.start();
private function readPreAnswer( evt:TimerEvent ):void
{
Alert.show("Time reach to this function.");
}
计时功能:
private var hour_timer:Number=0;
private var minute_timer:Number=0;
private var second_timer:Number=0;
private var handleTimer:Timer;
private var currentHours:String;
public function timerInit():void
{
this.hour_timer = Number("2");
this.minute_timer = Number("00");
this.second_timer = Number("00");
handInTimer = new Timer( 1000 );
handInTimer.addEventListener( "timer" , timerHandler);
handInTimer.start();
}
private function timerHandler(event:TimerEvent):void
{
second_timer--;
if (second_timer<0){second_timer=59;minute_timer--;}
if (minute_timer<0){minute_timer=59;hour_timer--;}
currentHours="0"+String(hour_timer)+":";
if (minute_timer<10){currentHours+="0"+String(minute_timer)+":";}else{currentHours+=String(minute_timer)+":";}
if (second_timer<10){currentHours+="0"+String(second_timer);}else{currentHours+=String(second_timer);}
}
currentHour即会随着时间的推移开始从2:00:00倒数,要想让计时停止,只要调用handInTimer.stop()即可。
------------------------------------------------
Felix原创,转载请注明出处,感谢博客园!
posted on 2009-03-27 19:45 Felix Fang 阅读(2942) 评论(1) 编辑 收藏 举报