Java與Flex學習筆記(14)—-Flex中實現倒計時的效果

來源:互聯網
上載者:User

      之前想做個倒計時的效果,本想著挺容易的,沒想到做起來還是有一些難度的,最後還是藉助網路資源實現了這個效果。


      我原想的是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/





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.