Actionscript 倒计时组件

/**
 * ComingX.com
 * Copyright 2012. All rights reserved.
 *
 * @Author: Jingle
 * @Email: jingdongemail#gmail.com
 * @Created date: 2012-2-6
 */
package com.jingle.view.component
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextLineMetrics;
	import flash.utils.Timer;
	import mx.controls.Label;
	import mx.core.UIComponent;
 
	[Event(name="timeUp", type="flash.events.Event")]
	public class TimeCounter extends UIComponent
	{
		private var timer:Timer;
		private var timeLabel:Label;
		/**
		 * 剩余时间,毫秒单位 
		 */		
		private var leftTime:Number;
 
		/**
		 * 时间到之后显示替代文本 
		 */		
		private static const TIME_UP:String = "时间到";
 
		/**
		 * 结束时间,毫秒单位
		 */
		private var _endTime:Number;
 
		/**
		 * 字体大小 
		 */		
		public var fontSize:Number = 20;
		/**
		 * 字体颜色 
		 */		
		public var fontColor:uint = 0x000000;
 
		public function TimeCounter()
		{
			super();
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
 
		}
 
		public function get endTime():Number{
			return this._endTime;
		}
 
		public function set endTime(value:Number):void
		{
			if(value != this._endTime && value)
			{
				_endTime = value;
				var timeNow:Number = (new Date()).getTime();
				leftTime = _endTime - timeNow;
				timer.stop();
				timer.reset();
				timer.start();
			}
		}
 
		override protected function createChildren():void
		{
			if(!timeLabel)
			{
				timeLabel = new Label();
				addChild(timeLabel);
				this.setStyle("fontSize",fontSize);
				this.setStyle("color",fontColor);
				var lineMatrics:TextLineMetrics = measureText("00 : 00 : 00");
				timeLabel.width = lineMatrics.width + 10;
				timeLabel.height = lineMatrics.height + 4;
				timeLabel.setStyle("textAlign","center");
				timeLabel.setStyle("verticalAlign","middle");
			}
 
			timer.start();
		}
 
		protected function onTimer(evt:TimerEvent):void
		{
			leftTime = leftTime - 1000;
			if(leftTime <= 0)
			{
				//到时间了
				timer.stop();
				dispatchEvent(new Event("timeUp"));
			}
			updateTimeLabel();
		}
 
		protected function updateTimeLabel():void
		{
			if(leftTime <= 0)
			{
				timeLabel.text = TimeCounter.TIME_UP;
				return;
			}
			var h:Number = Math.floor(leftTime / (1000 * 60 * 60) );
			var m:Number = Math.floor(leftTime % (1000 * 60 * 60) / (1000 * 60));
			var s:Number = Math.floor(leftTime % (1000 * 60) / 1000);
 
 
			timeLabel.text = oneToTow(h) + " : " + oneToTow(m) + " : " + oneToTow(s);
		}
 
		private function oneToTow(value:Number):String
		{
			var str:String;
			if(value < 10)
			{
				str = "0" + value;
			}
			else
			{
				str = "" + value;
			}
			return str;
 
		}
	}
}

使用方法

<component:TimeCounter endTime="{model.deadline}" fontSize="30" timeUp="timeUpHandler(event)" />

博文链接

更多AS Flex博文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值