Preloading my Life

This has got to be the longest preloader ever – the amount of time I have left until I’m out of the Navy:

[kml_flashembed movie=”/wp-content/uploads/2008/01/lifeloader.swf” height=”100″ width=”250″ fversion=”9″ /]

Which reminds me – here’s a handy AS3 class to create a countdown from now until some chosen date (passed as constructor argument):

CountDown.as

package com.onebyonedesign.utils {     

	public class CountDown {     

		private var _targetDate:Date;     

		public function CountDown(d:Date):void { 
			_targetDate = d; 
		}     

		/** 
		 * 
		 * @return	An object with the properties: days, hours, minutes, and seconds 
		 */ 
		public function getTime():Object { 
			var now:Date = new Date(); 
			var secs:Number = (_targetDate.getTime() - now.getTime()) / 1000; 
			return secondsToTime(secs); 
		}     

		private function secondsToTime(secs:Number):Object { 
			var min:Number = Math.floor(secs / 60); 
			var hour:Number = Math.floor(min / 60); 
			var day:Number = Math.floor(hour / 24); 
			min = min % 60; 
			hour = hour % 24; 
			var sec:Number = Math.floor(secs % 60);      

			var days:String = day < 10 ? "0" + day : day.toString(); 
			var seconds:String = sec < 10 ? "0" + sec : sec.toString(); 
			var minutes:String = min < 10 ? "0" + min : min.toString(); 
			var hours:String = hour < 10 ? "0" + hour : hour.toString();      

			return {days:days, hours:hours, minutes:minutes, seconds:seconds}; 
		} 
	} 
}

Quick usage:

package {     

	import com.onebyonedesign.utils.CountDown; 
	import flash.display.Sprite; 
	import flash.events.TimerEvent; 
	import flash.utils.Timer;     

	public class CountdownExample extends Sprite {     

		private var birthday:Date = new Date(2008, 11, 28); 
		private var cd:CountDown;     

		public function CountdownExample():void { 
			cd = new CountDown(birthday); 
			beginCount(); 
		}     

		private function beginCount():void { 
			var timer:Timer = new Timer(10000); 
			timer.addEventListener(TimerEvent.TIMER, showCount); 
			timer.start(); 
		}     

		private function showCount(te:TimerEvent):void { 
			var time:Object = cd.getTime(); 
			trace ("Only " + time.days + " days until Devon's birthday!"); 
		} 
	} 
}