I’ve seen a handful of third-party countdowns and a lot of them are overly complicated or don’t have the features I want. I created a slimmed-down version that has the following features:

  • Counts down to specific date OR specific number of seconds
  • Dispatches an event every second it updates and when the timer is finished
  • Returns an object that contains the days, hours, minutes, and seconds remaining



Here is the code for the class, with an example of how to use it:

package  com{
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;

	public class CountDown extends EventDispatcher{

		private var secondsRemaining:int;
		private var _timeRemaining:Object = {}
		private var countdownTimer:Timer;
			@ Author: Andrew Keith
			@ Website: www.andrewkeith.com
			@ Ver: 1.0 - 12.5.2013
			@ License: Do whatever you want with it, commercial or free, its for the world to use 🙂


		This countdown does all the math to calculate how many days/hours/minutes/seconds are left in a specific countdown.
			* You can countdown to a specified date in time or for a specific amount of seconds
			* Create a new instance of countdown
			* Set the target seconds or target date
			* Add a listener for Event.CHANGE (each second update) and Event.COMPLETE (when timer is up)
			* On each update, get the object "timeRemaining" from the clock. This will return an object with the days, hours, minutes, seconds left
			* Use this info however you want 🙂


		private function init(evt:Event):void
			cdc = new CountDown();
				//cdc.TARGET_SECONDS = 120; //Use this if you want to countdown to a specific # of seconds
			cdc.TARGET_DATE = new Date(2013, 11, 7);
			cdc.addEventListener(Event.CHANGE, onUpdate);
			cdc.addEventListener(Event.COMPLETE, onComplete)

		private function onUpdate(evt:Event):void
			var timeRemaining:Object = cdc.timeRemaining;
			trace("hours:", timeRemaining.hours, "minutes:", timeRemaining.minutes, "seconds:", timeRemaining.seconds);

		private function onComplete(evt:Event):void
			trace ("Times up!");


		public function CountDown() {
			countdownTimer = new Timer(1000);
			countdownTimer.addEventListener(TimerEvent.TIMER, onCountdown);

		public function set TARGET_SECONDS(seconds:int):void
			secondsRemaining = seconds;

		public function set TARGET_DATE(target:Date):void
			var currentDate:Date = new Date();
			var currentSeconds = currentDate.valueOf();

			var targetSeconds = target.valueOf();

			var difference:int = (targetSeconds - currentSeconds) / 1000;
			secondsRemaining = difference;

		public function START():void

		public function STOP():void

		private function onCountdown(evt:TimerEvent):void
			var days:int = Math.floor(secondsRemaining/(3600*24));
			var hours:int = Math.floor((secondsRemaining / 3600)%24);
			var minutes:int = Math.floor((secondsRemaining / 60) % 60);
			var seconds:int = secondsRemaining%60;

			timeRemaining.days = days;
			timeRemaining.hours = hours;
			timeRemaining.minutes = minutes;
			timeRemaining.seconds = seconds;

			if(secondsRemaining < 0)
				dispatchEvent(new Event(Event.COMPLETE, true));
				countdownTimer.removeEventListener(TimerEvent.TIMER, onCountdown);
				dispatchEvent(new Event(Event.CHANGE, true));

		public function get timeRemaining():Object
			return _timeRemaining;