Timer类的使用 以及 setInterval()方法的使用
1. Timer类的使用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" creationComplete="init()">
<mx:Script>
<![CDATA[
import flash.utils.Timer;
private function init():void
{
// 创建一个执行10次,每秒一次的Timer对象
var timer:Timer = new Timer(1000, 10);
timer.addEventListener(TimerEvent.TIMER, doEach);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, doComplete);
timer.start();
}
private function doEach(event:TimerEvent):void
{
// 输出每一次的计数值
trace(event.target.currentCount);
}
private function doComplete(event:TimerEvent):void
{
trace("Time over...");
}
]]>
</mx:Script>
</mx:Application>
运行结果:
1
2
3
4
5
6
7
8
9
10
Time over...
2. setInterval()方法的使用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()" fontSize="12">
<mx:Script>
<![CDATA[
import flash.utils.setInterval;
import mx.controls.Alert;
private var count:Number;
private var i:int = 1000;
private var interval:uint;
public function init():void
{
count = 20;
interval = setInterval(function():void
{
showLabel.text = "台湾回归倒计时: " + count.toString() + " 秒";
count -= 1;
if(count < 0)
{
clearInterval(interval);
showLabel.text="台湾回归啦!"
}
},1000);
}
]]>
</mx:Script>
<mx:Label id="showLabel"/>
</mx:Application>