Home > ActionScript 3.0 Basics > Countdown timer with ActionScript 3.0

Countdown timer with ActionScript 3.0





In this tutorial I will show you how to use Timer and Date classes to create a nice countdown timer using Actionscript 3.

Well, each of us is waiting for something nice – birthday, vacation, weekend. As a soccer fan I am waiting for the FIFA World Cup 2010 which takes place in South Africa and begins on 11th June 2010. I am counting down the days but I can say it’s not that easy. Because of this I made a Flash countdown timer and now it is much easier.

So let’s get started with our countdown timer.

1. Create a new Flash CS3 document and save it under the name “countdown.fla”.
2. Now create a new dynamic text field and name it “time_txt”. Here we’ll put the time that remains to the beginning of the world cup championship. For this tutorial I am using font downloaded from here. To fully complete this tutorial you have to download this font and to install it on your system. Choose #009900 for font color and 24 for font size. Embed the font like this:

Character embedding

3. Now we have to move to actionscript. Create a new layer called “actions” and on its first keyframe press F9 to go to the “Actions” panel. Here is the code:

//First we create a date object. Note that we count the months from "0"!
var endDate:Date = new Date(2010, 5, 11);
//next we create a timer that will update the time every second
var timer:Timer = new Timer(1000);
//we add an event listener to the timer and then we start it
timer.addEventListener(TimerEvent.TIMER, updateTime);
timer.start();
//Calculate the time remaining as it is being updated
function updateTime(e:TimerEvent):void{
	//we create a variable to hold the current date
	var currentDate:Date = new Date();
	//we calculate the time to FIFA World Cup 2010
	var timeLeft:Number = endDate.getTime() - currentDate.getTime();
	//we convert the remaining time into seconds, minutes, hours, and days
	var seconds:Number = Math.floor(timeLeft / 1000);
	var minutes:Number = Math.floor(seconds / 60);
	var hours:Number = Math.floor(minutes / 60);
	var days:Number = Math.floor(hours / 24);

	seconds %= 60;
	minutes %= 60;
	hours %= 24;

	//we convert the numbers to strings
	var sec:String = seconds.toString();
	var min:String = minutes.toString();
	var hrs:String = hours.toString();
	var d:String = days.toString();

	//if we have a single digit we add "0" in front of it
	if (sec.length < 2) {
	    sec = "0" + sec;
	}

	if (min.length < 2) {
	    min = "0" + min;
	}

	if (hrs.length < 2) {
	    hrs = "0" + hrs;
	}

	//we create a variable to hold all elements and then we add it to our text field
	var time:String = d + ":" + hrs + ":" + min + ":" + sec;
	time_txt.text = time;
}

And here is the final result:

Liked this tutorial? Share and Enjoy:

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Reddit
  • blogmarks
  • StumbleUpon
  • Technorati
  • TwitThis
  • Yahoo! Buzz
  • NewsVine
  • Slashdot
  1. June 25th, 2009 at 12:52 | #1

    Very nice of you to show the usage of actionscript 3 for the creation of a simple countdown timer. It looks very familiar to me, though. I am going to check up every once in a while for updates on your blog. Keep up the good work.

  2. July 7th, 2009 at 04:57 | #2

    You know so many interesting infomation. You might be very wise. I like such people. Don’t top writing.

  3. August 3rd, 2009 at 06:00 | #3

    Thank you for the code above. Can you explain what code I would use if I just wanted to have the timer in minutes ONLY?

    Thanks,
    Tony

  4. admin
    August 4th, 2009 at 10:20 | #4

    Sure.
    Remove this part of the code: minutes %= 60;
    You should do this because you want to show all of the minutes.
    Then change this:
    var time:String = d + “:” + hrs + “:” + min + “:” + sec;
    to:
    var time:String = min;

    That’s all.

  5. Jark
    November 16th, 2009 at 11:01 | #5

    Hi, how would you add code for milliseconds?
    Awesome job and thanks in advance!

  6. saeef
    February 5th, 2010 at 11:22 | #6

    how can i set the target date through an external xml file.

    plz, help me ASAP.
    thanks
    saeef

  7. Steve
    April 6th, 2010 at 00:59 | #7

    Can someone PLEASE tell me how to do a simple (days only) count down? I need it to count down to April 19, 2010. THANKS so much for any help. I tried modifying this tutorial, but no luck.

    Thanks

  8. Steve
    April 6th, 2010 at 15:21 | #8

    Every time I make a change to the line
    var endDate:Date = new Date(2010, 5, 11);
    The result never adds up to the right day. I put April 19th 2010 and it says there is 65 days remaining. Should be more like 13 days.

    Thanks for your help.

  9. April 13th, 2010 at 03:15 | #9

    I want, date with hours: example

    countdown to 18 april 2010 at 5:00 pm

    Please can anybody help me with this :(

  10. May 6th, 2010 at 18:06 | #10

    Nice work its awesome

  11. Luis Felipe
    May 31st, 2010 at 15:02 | #11

    How i stop when countdown finish the? Because she is giving number negative finish after

  12. July 25th, 2010 at 14:11 | #12

    @Luis Felipe
    Hi Luis

    I had the same problem until I came across another tutorial which showed how to stop the counter at 0.

    if(time_txt.text == “0:00:00:00″){
    delete this.onEnterFrame;
    }
    The above depends on whether you are using days:hrs:mins:secs

    Hope it helps

  13. July 25th, 2010 at 14:17 | #13

    @Steve

    In your code of… new Date(2010, 5, 11);

    You are setting the date to be 2010, June 11th

    If you use numbers for months remember that months start at “0″ not 1

    So January = 0
    February = 1
    March = 2
    April = 3
    etc

    as you wanted April 19th 2010
    you shd have had… new Date(2010, 3, 19);

  14. Chris Dunn
    December 13th, 2010 at 17:37 | #14

    Hi I am after a 2 minute countdown timer in Action Script 3 can anybody help me.

    Thank you

    Chris

  15. Srinevasan
    March 21st, 2011 at 15:15 | #15

    How can i update the hours

  16. TheDarkIn1978
    August 26th, 2011 at 05:45 | #16

    @Ron, stopping the timer by polling if the string is “0:00:00:00″ will only work if the application is running when that happens. reloading the swf will still generate a negative timer. the solution is to simply compare the two dates to determine if the currentData is later (or greater than) the endData:

    if (currentDate.getTime() > endDate.getTime())
    time_txt.text = “0:00:00:00″;
    else
    time_txt.text = time;

  17. WOMBAT
    September 7th, 2011 at 06:36 | #17

    i have 2 timers running at once to display time until each game for the rugby world cup how can i get each timer to stop when it reaches zero. And display zero or a message while letting the other timers to continue running any help would be great thanks

    this.onEnterFrame = function() {

    var today:Date = new Date () ;
    var currentYear = today.getFullYear ();
    var currentTime = today.getTime () ;

    var targetDate:Date = new Date(currentYear,8,7,12,17);
    var targetTime = targetDate.getTime () ;

    var timeLeft = targetTime – currentTime;

    var sec = Math.floor(timeLeft/1000);
    var min = Math.floor(sec/60);
    var hrs = Math.floor(min/60);
    var days = Math.floor(hrs/24);
    sec = string(sec % 60);
    if (sec.length < 2) {
    sec = "0" + sec;
    }
    min = string (min % 60) ;
    if (min.length < 2) {
    min = "0" + min;
    }
    hrs = string (hrs % 24);
    if (hrs.length < 2) {
    hrs = "0" + hrs;
    }
    days = string(days) ;

    var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
    time_txt.text = counter;

    var today:Date = new Date () ;
    var currentYear = today.getFullYear ();
    var currentTime = today.getTime () ;

    var targetDate:Date = new Date(currentYear,8,17,18,00);
    var targetTime = targetDate.getTime () ;

    var timeLeft = targetTime – currentTime;

    var sec = Math.floor(timeLeft/1000);
    var min = Math.floor(sec/60);
    var hrs = Math.floor(min/60);
    var days = Math.floor(hrs/24);
    sec = string(sec % 60);
    if (sec.length < 2) {
    sec = "0" + sec;
    }
    min = string (min % 60) ;
    if (min.length < 2) {
    min = "0" + min;
    }
    hrs = string (hrs % 24);
    if (hrs.length < 2) {
    hrs = "0" + hrs;
    }
    days = string(days) ;

    var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
    time_txt1.text = counter;

    }

  18. November 8th, 2011 at 11:14 | #18

    @Ron Pumfleet

    THANKS!!! i wondered why the date is always wrong….

  1. December 29th, 2010 at 07:45 | #1