之前想做個倒計時的效果,本想著挺容易的,沒想到做起來還是有一些難度的,最後還是藉助網路資源實現了這個效果。
我原想的是flex中有一個setTimeout()函數,此函數意思是在指定的延遲(以毫秒為單位)後運行指定的函數,即
public function setTimeout(closure:Function, delay:Number,... arguments):uint
但是如果做成倒計時時,只減少一個數,不知道是不是因為flex運行一幀的緣故。無奈,只好作罷。通過改造網上的一個倒計時鐘的資源實現了倒計時的效果,在此表示感謝。建立一個mxml檔案,且看原檔案的如下部分所示代碼:
<?xml version="1.0"encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" preloader="ases.util.LoadingProgressBarUtil" width="100%" height="100%"creationComplete="app_creationCompleteHandler(event)"> <fx:Style source="css/activateEmail.css" /> <fx:Script> <![CDATA[ public var timer:Timer; [Bindable]public var str:String; protected function app_creationCompleteHandler(event:FlexEvent):void { timer=new Timer(1000); //設定間隔時間為1s timer.addEventListener(TimerEvent.TIMER, timehandle) } //調用倒計時方法 private function startTimer():void { timer.start(); } //停止倒計時方法 private function stopTimer():void { timer.stop(); } // private function timeTransform(stattime:int, counter:int):String { var str:String=""; var count:int=stattime - counter; var second:int=count % 60; str=(second + "")+"s後返回首頁..."; if (second == 0) { isStop=true; //可在此處添加倒計時末期的處理方法。 str="請稍候,正在跳轉中..." var url:URLRequest=new URLRequest("http://localhost:9080/mydisk/swf/login.html"); navigateToURL(url,"_self"); } else { isStop=false; } return str; } private function timehandle(e:TimerEvent):void { var count:int=timer.currentCount; str=timeTransform(6, count); if (isStop) { stopTimer(); } } <s:Labelid="back" text="{str}"/></s:Application>
上面貼出了實現倒計時的代碼,可以看出實現這個效果是首先執行個體化一個Timer對象,然後通過監聽Timer來實現效果。當我們需要顯示此倒計時時調用startTimer()方法,反之調用stopTimer()方法即可。部分注釋詳見代碼。
效果如下所示:
原創文章,轉載請註明出處:http://www.dianfusoft.com/