flash+AS3制作的倒计时效果

上周由于工作需要,使用flash+AS3开发的倒计时显示效果。


具体的实现代码:

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.net.URLRequest;

function doubleString(time:int):String{
	var r:String=time.toString();
	
	if(r.length==1){
		return "0"+r;
	}else{
		return r;
	}
}

function goRedirect(evt:MouseEvent):void {
	var req:URLRequest = new URLRequest("http://xxx.domain.com/greatmessage");
	navigateToURL(req,'_blank');
}

var ticker:Timer = new Timer(1000);
ticker.addEventListener(TimerEvent.TIMER, onTicker);
ticker.start();

tmBtn.addEventListener(MouseEvent.CLICK, goRedirect);

var downDate:Date = new Date(2011,3,22,24,0,0); //月份是从0开始计数的

function onTicker(evt:TimerEvent):Boolean {
	var currentDate:Date = new Date();
				var currentTime:Number = currentDate.valueOf();
				
				var endTime:Number = downDate.valueOf();
				
				var currentOffset:Number = currentDate.getTimezoneOffset();
				
				var minusN:Number = endTime - currentTime;
				
				//check if we started
				if( minusN <= 0) 
				{
					restime_day.text = '0';
					restime_hour.text = '00';
					restime_minute.text = '00';
					restime_second.text = '00';
					return true;
				}
				
				var minusSecond:int = downDate.getSeconds() - currentDate.getSeconds();
				var minusMinute:int;
				var minusHour:int;
				var minusDate:int;
				var minusMonth:int;
				var minusYear:int;
				
				var sFlag:Boolean;
				var mFlag:Boolean;
				var hFlag:Boolean;
				var monFlag:Boolean;
				
				if(minusSecond < 0){
					minusSecond = 60+minusSecond;
					sFlag = true;			
				}
				
				minusMinute = downDate.getMinutes() - currentDate.getMinutes();
				if(sFlag)
					minusMinute--;
				
				if(minusMinute < 0){
					minusMinute = 60 + minusMinute;
					mFlag = true;
				}			
				
				minusHour = downDate.getHours() - currentDate.getHours();
				if(mFlag)
					minusHour--;
				
				if(minusHour < 0){
					minusHour = 24 + minusHour;
					hFlag = true;
				}
				
				minusDate  = downDate.getDate() - currentDate.getDate();
				minusMonth = downDate.getMonth() - currentDate.getMonth();
				
				if(hFlag){
					minusDate--;
				}
				
				if(minusDate < 0){
					switch(currentDate.getMonth() + 1){
						case 1,3,5,7,8,10,12:{
							minusDate = downDate.getDate() + (31- currentDate.getDate());
						}
							case 4,6,9,11:{
							minusDate = downDate.getDate() + (30- currentDate.getDate());
						}
						case 2:{
							if((currentDate.getFullYear()%4 == 0 && currentDate.getFullYear()%100 != 0)||(currentDate.getFullYear()%400 == 0)){//闰年
								minusDate = downDate.getDate() + (29 - currentDate.getDate());
							}else{
								minusDate = downDate.getDate() + (28- currentDate.getDate());
							}
						}
					}
					minusMonth--;
				}		
				
				if(minusMonth < 0){
					minusMonth = 12+minusMonth;
					monFlag = true;
				}
				
				minusYear = downDate.getFullYear() - currentDate.getFullYear();
				if(monFlag){
					minusYear--;
				}
				restime_day.text = String(minusDate);
				restime_hour.text = doubleString(minusHour);
				restime_minute.text = doubleString(minusMinute);
				restime_second.text = doubleString(minusSecond);
	
	return true;
}

 

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

你可能感兴趣的:(.net,工作,Flash)