首先,需要建一个Flex工程,打开mxml文件加入代码如下:
[code]
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
<mx:Text id="clock" text="" creationComplete="this.init();"
right="10" height="20" width="120" top="10"/>
<mx:Script>
<![CDATA[
import flash.utils.Timer;
import flash.events.TimerEvent;
private var timer:Timer;
private function init():void {
this.timer = new Timer(1000);
this.timer.addEventListener(TimerEvent.TIMER, this.resetNow);
this.timer.start();
}
private function resetNow(event:TimerEvent):void {
this.clock.text = new Date().toLocaleTimeString();
}
]]>
</mx:Script>
</mx:Application>
[/code]
即可实现数字时钟的效果,更新时间设置为1000ms(1s)
[code]
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
<mx:Text id="clock" text="" creationComplete="this.init();"
right="10" height="20" width="120" top="10"/>
<mx:Script>
<![CDATA[
import flash.utils.Timer;
import flash.events.TimerEvent;
private var timer:Timer;
private function init():void {
this.timer = new Timer(1000);
this.timer.addEventListener(TimerEvent.TIMER, this.resetNow);
this.timer.start();
}
private function resetNow(event:TimerEvent):void {
this.clock.text = new Date().toLocaleTimeString();
}
]]>
</mx:Script>
</mx:Application>
[/code]
即可实现数字时钟的效果,更新时间设置为1000ms(1s)