flash+AS3制作的倒计时效果

具体的实现代码:

Actionscript代码   收藏代码
  1. import flash.utils.Timer;  
  2. import flash.events.TimerEvent;  
  3. import flash.events.MouseEvent;  
  4. import flash.net.URLRequest;  
  5.   
  6. function doubleString(time:int):String{  
  7.     var r:String=time.toString();  
  8.       
  9.     if(r.length==1){  
  10.         return "0"+r;  
  11.     }else{  
  12.         return r;  
  13.     }  
  14. }  
  15.   
  16. function goRedirect(evt:MouseEvent):void {  
  17.     var req:URLRequest = new URLRequest("http://xxx.domain.com/greatmessage");  
  18.     navigateToURL(req,'_blank');  
  19. }  
  20.   
  21. var ticker:Timer = new Timer(1000);  
  22. ticker.addEventListener(TimerEvent.TIMER, onTicker);  
  23. ticker.start();  
  24.   
  25. tmBtn.addEventListener(MouseEvent.CLICK, goRedirect);  
  26.   
  27. var downDate:Date = new Date(2011,3,22,24,0,0); //月份是从0开始计数的  
  28.   
  29. function onTicker(evt:TimerEvent):Boolean {  
  30.     var currentDate:Date = new Date();  
  31.                 var currentTime:Number = currentDate.valueOf();  
  32.                   
  33.                 var endTime:Number = downDate.valueOf();  
  34.                   
  35.                 var currentOffset:Number = currentDate.getTimezoneOffset();  
  36.                   
  37.                 var minusN:Number = endTime - currentTime;  
  38.                   
  39.                 //check if we started  
  40.                 if( minusN <= 0)   
  41.                 {  
  42.                     restime_day.text = '0';  
  43.                     restime_hour.text = '00';  
  44.                     restime_minute.text = '00';  
  45.                     restime_second.text = '00';  
  46.                     return true;  
  47.                 }  
  48.                   
  49.                 var minusSecond:int = downDate.getSeconds() - currentDate.getSeconds();  
  50.                 var minusMinute:int;  
  51.                 var minusHour:int;  
  52.                 var minusDate:int;  
  53.                 var minusMonth:int;  
  54.                 var minusYear:int;  
  55.                   
  56.                 var sFlag:Boolean;  
  57.                 var mFlag:Boolean;  
  58.                 var hFlag:Boolean;  
  59.                 var monFlag:Boolean;  
  60.                   
  61.                 if(minusSecond < 0){  
  62.                     minusSecond = 60+minusSecond;  
  63.                     sFlag = true;             
  64.                 }  
  65.                   
  66.                 minusMinute = downDate.getMinutes() - currentDate.getMinutes();  
  67.                 if(sFlag)  
  68.                     minusMinute--;  
  69.                   
  70.                 if(minusMinute < 0){  
  71.                     minusMinute = 60 + minusMinute;  
  72.                     mFlag = true;  
  73.                 }             
  74.                   
  75.                 minusHour = downDate.getHours() - currentDate.getHours();  
  76.                 if(mFlag)  
  77.                     minusHour--;  
  78.                   
  79.                 if(minusHour < 0){  
  80.                     minusHour = 24 + minusHour;  
  81.                     hFlag = true;  
  82.                 }  
  83.                   
  84.                 minusDate  = downDate.getDate() - currentDate.getDate();  
  85.                 minusMonth = downDate.getMonth() - currentDate.getMonth();  
  86.                   
  87.                 if(hFlag){  
  88.                     minusDate--;  
  89.                 }  
  90.                   
  91.                 if(minusDate < 0){  
  92.                     switch(currentDate.getMonth() + 1){  
  93.                         case 1,3,5,7,8,10,12:{  
  94.                             minusDate = downDate.getDate() + (31- currentDate.getDate());  
  95.                         }  
  96.                             case 4,6,9,11:{  
  97.                             minusDate = downDate.getDate() + (30- currentDate.getDate());  
  98.                         }  
  99.                         case 2:{  
  100.                             if((currentDate.getFullYear()%4 == 0 && currentDate.getFullYear()%100 != 0)||(currentDate.getFullYear()%400 == 0)){//闰年  
  101.                                 minusDate = downDate.getDate() + (29 - currentDate.getDate());  
  102.                             }else{  
  103.                                 minusDate = downDate.getDate() + (28- currentDate.getDate());  
  104.                             }  
  105.                         }  
  106.                     }  
  107.                     minusMonth--;  
  108.                 }         
  109.                   
  110.                 if(minusMonth < 0){  
  111.                     minusMonth = 12+minusMonth;  
  112.                     monFlag = true;  
  113.                 }  
  114.                   
  115.                 minusYear = downDate.getFullYear() - currentDate.getFullYear();  
  116.                 if(monFlag){  
  117.                     minusYear--;  
  118.                 }  
  119.                 restime_day.text = String(minusDate);  
  120.                 restime_hour.text = doubleString(minusHour);  
  121.                 restime_minute.text = doubleString(minusMinute);  
  122.                 restime_second.text = doubleString(minusSecond);  
  123.       
  124.     return true;  
  125. }  

 

上述代码只能使用AS3.0来实现,其中,Timer定时器类只在AS3.0才实现,1.0、2.0版本时,主要使用setInterval来实现的。

你可能感兴趣的:(flash+AS3制作的倒计时效果)