Flash simple timer/countdown

Sometimes you need a timer or a countdown... for a page, in a game or whatsoever.

So I am going to show you how to create a timer or a countdown. Or both.

And, in next tutorials, how to use it in the games we are creating, of course...

Look at this movie:

On the left we have a timer, on the right a countdown.

Let's see how to do it.

First, I created two dynamic text items, the one on the left instanced as count and the one on the right instanced as count_down.

Then, in the first frame there is this simple actionscript:

ACTIONSCRIPT:
  1. start_time = getTimer();
  2. countdown = 7200000;
  3. onEnterFrame = function () {
  4.     elapsed_time = getTimer()-start_time;
  5.     _root.count.text = time_to_string(elapsed_time);
  6.     _root.count_down.text = time_to_string(_root.countdown-elapsed_time);
  7. };
  8. function time_to_string(time_to_convert) {
  9.     elapsed_hours = Math.floor(time_to_convert/3600000);
  10.     remaining = time_to_convert-(elapsed_hours*3600000);
  11.     elapsed_minutes = Math.floor(remaining/60000);
  12.     remaining = remaining-(elapsed_minutes*60000);
  13.     elapsed_seconds = Math.floor(remaining/1000);
  14.     remaining = remaining-(elapsed_seconds*1000);
  15.     elapsed_fs = Math.floor(remaining/10);
  16.     if (elapsed_hours<10) {
  17.         hours = "0"+elapsed_hours.toString();
  18.     } else {
  19.         hours = elapsed_hours.toString();
  20.     }
  21.     if (elapsed_minutes<10) {
  22.         minutes = "0"+elapsed_minutes.toString();
  23.     } else {
  24.         minutes = elapsed_minutes.toString();
  25.     }
  26.     if (elapsed_seconds<10) {
  27.         seconds = "0"+elapsed_seconds.toString();
  28.     } else {
  29.         seconds = elapsed_seconds.toString();
  30.     }
  31.     if (elapsed_fs<10) {
  32.         hundredths = "0"+elapsed_fs.toString();
  33.     } else {
  34.         hundredths = elapsed_fs.toString();
  35.     }
  36.     return hours+":"+minutes+":"+seconds+":"+hundredths;
  37. }

Line 1: Initialize the variable start_time with the current time value

Line 2: Initialize the variable countdown with the amount of milliseconds I want to start the countdown. In this case, since an hour has 3600 seconds or 3600000 milliseconds, the countdown will start from 2 hours

Line 3: Function to be executed at every frame

Line 4: Calculate the difference between the actual time and the start_time value. This is actually the elapsed time.

Lines 5-6: count and count_down text values are updated with the result of a the function time_to_string (a function I created). count will be the elapsed time, while count_down will be the difference from the countdown variable and the elapsed time. Please note that every time explained here is intended to be in milliseconds.

Let's examine the main function:

Line 9: Calculate elapsed hours dividing the time to convert (function's argument) by 3600000, the number of milliseconds in an hour

Line 10: Calculate remaining time without the elapsed hours

Lines 11-12: Some thing with the minutes, dividing the remaining time by 60000, the number of milliseconds in a minute

In the same way I calculate the seconds and the 1/100 of seconds. Lines from 16 to 35 just format the string showing the time.

This is a very simple function we will use in the games we are going to create, such as "line rider".

Download the source code and give me feedback.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (14 votes, average: 4.64 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

72 Responses to “Flash simple timer/countdown”

  1. Algis on February 19th, 2007 10:32 pm

    Still using AS 1.0 ?

  2. bigbill on February 20th, 2007 9:29 pm

    Great Timers, quite handy however i’ve tried to put the following code into and can’t get it to work.It just keeps on counting down. Can anyone help please?

    if (_root.count_down

  3. bigbill on February 20th, 2007 9:31 pm

    That should be –
    if(_root.count_down

  4. mousey on February 22nd, 2007 10:53 am

    Cool, really easy and really fun >!

  5. bigbill on February 22nd, 2007 7:41 pm

    How do you goto another frame when the countdown reaches 0?
    btw Have problems posting.

  6. Alex on February 23rd, 2007 3:47 am

    nice, i used it in one of my games

  7. Richard on February 27th, 2007 9:22 am

    Hi. Im Making a linerider game. its basicly finished. ive made it so that you can draw a line, the ball bounces perfectly off it. but i still dont get something. when you’ve finished drawing:

    onMouseUp = function () {
    imdrawing = false;
    };

    that code doesnt want to work :S any tips

    Richard

    PS: this code is RIGHT at the end of the code. is that right or should it be elseware

  8. brisa on March 1st, 2007 11:56 pm

    Thanks for posting this. I used part of the code to fix my counter and it works perfect.

    Nice work:)

    Brisa

  9. abhilash on March 2nd, 2007 12:01 pm

    nice tutorial

  10. lister on March 22nd, 2007 1:16 am

    That’s great!

    Is there a simple way to make a start/stop & reset button for this?

  11. danny o'dwyer on April 16th, 2007 5:14 pm

    im in the same situation as big bill.

    how can you set a limit, so it terminated when it hits a certain time, i cant get it to work.

  12. tom on April 17th, 2007 4:24 pm

    to those who were having problems with going to another frame after the timer hits zero, try adding this…

    if (_root.countdown-elapsed_time

  13. tom on April 17th, 2007 4:44 pm

    or instead of using “gotoAndPlay()”, maybe try using “gotoAndStop”.

    seems to work better for me.

    - tom

  14. pocoster on April 20th, 2007 9:24 pm

    ive been puzzled about it going to another frame after zero too ive tried many different ways of doing it and none hav worked anyone got a solution please?

  15. Chandler on April 24th, 2007 7:09 pm

    how do I get it to stop on O? I’ve tried everything I can think of but can’t get it working…

  16. Neo on May 18th, 2007 12:14 am

    Is there an easy way to have the counter start at a certain time. Example: 06:44:33

    6 hours, 44 minutes, 33 seconds

    Thanks

  17. Anavrin on June 26th, 2007 12:13 pm

    if(hours == 0 && minutes == 0 && seconds == 0) {
    delete this.onEnterFrame;
    gotoAndPlay(2);
    }
    \\ stops the countdown at 0
    \\goes to frame 2

    setting line 2 to:
    countdown = 24273000 ;
    would start the countdown clock at 6 hours 44 minutes 33 seconds

  18. nor on July 4th, 2007 12:48 pm

    hey i just wonder why the code that u give is different from the downloaded file. The code that u give is not working but then the downloaded file is working fine.

    thanx 4 the codes. It’s useful. =)

    have a nice day!

  19. cpmartin on July 9th, 2007 4:37 pm

    How could this be converted into a Two minute timer? Any help is much appreciated. Thank you in advance.

  20. CF on July 10th, 2007 5:18 pm

    This is really great. I am a newbie and can’t customize the code for what I would like to accomplish: a simple seconds countdown from 10 to 0 seconds. The countdown should stop at 0. Any easy way to accomplsih this without a lot of code (head spinning);-)

    Thanks!

  21. Jfulton on July 11th, 2007 9:08 pm

    i was wondering how you could get the color of the numbers to change color as you get closer to zero. Almost as if your time was running out.

  22. Jfulton on July 12th, 2007 1:47 am

    Also i cant seem to get the timer to stop. Can anyone help me with this?

  23. kMan on July 30th, 2007 5:03 pm

    A simple solution to stop the ticker would be:

    if (_root.count_down.text == “00:00″){
    gotoAndStop(2);
    }

    And if you want the ticker to stop when reaching all zeros just put a static text containing zeros in frame 2.

    Note: if (_root.count_down.text == “00:00″){
    the zeros must be equal to what you want to display, my example is based on minutes and seconds showing in the count down.

    Hope this will help…

  24. uncle gilbo on August 9th, 2007 7:31 am

    so if i wanted to make a countdown that started @ 2 min would i clear out the elasped hours code and replace the 60000 seconds with 120 and the 6000 min with 2 min?…code is all new to me…sorry if this seems dumb…

  25. Sunil kumar on August 10th, 2007 9:24 pm

    Hi
    Thanks it is working fine.

    Regards
    Sunil kumar

  26. Amanda on August 22nd, 2007 7:22 pm

    I’m having problems with script for stopping the countdown as well.

    I’m using this script for a 2 digit timer with only seconds:

    if (_root.count_down.text=”00″) {
    gotoAndPlay(3);
    }

    With this code in place, my movie entirely skips the countdown, and instead immediately jumps to frame 3. Any advice?

  27. Cristian on August 31st, 2007 11:33 am

    you must place the code like this ……..

    //The Code…..firs Frame….

    function time_to_string(time_to_convert)
    {
    elapsed_hours = Math.floor(time_to_convert / 3600000);
    remaining = time_to_convert – elapsed_hours * 3600000;
    elapsed_minutes = Math.floor(remaining / 60000);
    remaining = remaining – elapsed_minutes * 60000;
    elapsed_seconds = Math.floor(remaining / 1000);
    remaining = remaining – elapsed_seconds * 1000;
    elapsedH = Math.floor(remaining / 10);
    if (elapsed_hours

  28. Cristian on August 31st, 2007 11:35 am

    //in firs frame search for —>>on EnterFrame and replace the code with //this..

    onEnterFrame = function ()
    {

    elapsed_time = getTimer() – start_time;

    _root.count.text = time_to_string(elapsed_time);

    _root.count_down.text = time_to_string(_root.countdown – elapsed_time);

    if (_root.count_down.text == “00:00:00:00″) {
    play();
    };

    };
    stop();

  29. xyxo brocka on September 26th, 2007 8:16 pm

    i’m not much with codes. i can’t stop the ticker, i already tried all the suggestions here. perhaps the counter author might figure out a way to make the ticker stop at zero and jump to next scene.

    somehow, this code doesn’t work, doesn’t stop the ticker and doesn’t make the flash jump into a new scene. what seems to be the problem here sir?

    if (_root.count_down.text==”00:00″) {
    gotoAndStop(”start”, 1);
    }

  30. webmaster on October 24th, 2007 2:27 pm

    thanks for this counter. I will put in a website that i have in mind

  31. jr on October 24th, 2007 4:33 pm

    I want the timer to count down from example 16 min 41 sec, I don´t get how to do it?? Someone know how to do it?

  32. jr on October 25th, 2007 3:54 pm

    I got it.

  33. Valerij on October 27th, 2007 8:32 pm

    Hi!
    Im using this timer for a game, so the point of the game is to move your cursor through a labyrinth without touching the walls and I want to have a timer on this to see how fast people can do that, but then I need to stop it and view their final time (its in another frame) how do I do that, and if possible I would like to restart that timer again when I get to the next level and again when they finish it view they final overall time. I could work like the once on your Race game (round times and a overall time.

  34. hassan on December 20th, 2007 7:47 pm

    please guys, i want to stop timer from reading or counting, guys i want you to give me the codes or by sending it to my email box. thanks guys.

  35. hassan on December 20th, 2007 7:52 pm

    Also i cant stop, to get the timer to stop. Can anyone help me with this? this is my email box hassan4allofus@yahoo.com

  36. hussain on December 20th, 2007 7:54 pm

    Also i cant stop, to get the timer to stop. Can anyone help me with this? this is my email box muhammedhassan4islam@yahoo.com

  37. hassan on December 20th, 2007 8:02 pm

    please guys how can i remove timer from counting or reading,i meant by using shutdown, to broswe.please guys this is my email box hassan4allofus@yahoo.com. thanks.

  38. hassan on December 20th, 2007 8:09 pm

    please tom how can i apply the code,as you just saide early.please tom, this is my email box hassan4allofus@yahoo.com. thanks

  39. juan on February 13th, 2008 9:27 pm

    To stop on zero you must enter this code in onEnterFrame = function

    if (elapsed_time >= countdown) {
    gotoAndStop(2);
    };

    thanks for the code!

  40. jose on February 28th, 2008 6:43 pm

    How do you incorporate days into this? like a 30 day countdown…?

  41. Owen on March 8th, 2008 4:49 pm

    I’m trying to incorporate this into a game where you go to the “game over” frame after a hitTest. How do I get the timer to freeze when you go to the next frame, so people can see their finishing time?

  42. Jennifer on March 12th, 2008 9:03 pm

    I have the same question as Jose: How can I incorporate days into this?

  43. matt on March 24th, 2008 5:58 am

    i need help… how can i make it so that on a certain time something happens? here is my code so far
    onClipEvent (enterframe){
    if (timer_txt == “00:00:00:10″){
    _root.object.gotoAndPlay(6){
    }
    }
    why doesnt this work?
    email me at soggyoats@gmail.com

  44. Chris on April 2nd, 2008 4:58 pm

    For days I added this code:

    in the function time_to_string(time_to_convert)

    elapsed_days = Math.floor
    time_to_convert/86400000);

    remaining = time_to_convert-(elapsed_days*86400000);

    The wher the if statments begin this was added:

    if (elapsed_days<10) {
    days = “0″+elapsed_days.toString();
    } else {
    days = elapsed_days.toString();
    }

    then the “return” was adjusted to read:

    return days+” Days: “+hours+” Hrs: “+minutes+” Min: “+seconds+” Sec “+hundredths;

    So I have the days but now I want to limit the hous to only 24 instead of the total minutes remaining in say 10 days.
    for example my readout says:
    10 Days: 249 Hrs: so many Min: etc.. Sec:

    any suggestions?
    e-mail me at cslmke2000@yahoo.com

  45. dafydd1994 on April 8th, 2008 7:46 pm

    great tut, but is there a way that i can get the timer to stop when a object crosses a certian point.

  46. TG on May 7th, 2008 2:06 am

    Hey man, this is awesome. I love all the flash stuff you make!

  47. PiggyStudios on May 31st, 2008 7:03 pm

    put if.0>i++{gotoAndPlay(ur frame)}
    that should work

  48. Josh on June 1st, 2008 10:23 am

    thx juan … your advice on stopping the timer REALLY helped alot … thx

  49. Ter on June 9th, 2008 4:09 am

    Great countdown code. I’ve modified it to work for my purposes and it’s great. The only thing I can’t seem to figure out is how to pause it and restart it from where I’ve left off.

    I’ve created a Pause button that when clicked, grabs the current time displayed in the count_down textbox stores it in a textbox called pausedtime, hides the count_down text box, unhides the pausedtime textbox showing the time when the Pause button was clicked. Works great. The pause button also creates a var and stores the getTimer when the button is clicked.

    This also hides the Pause button and shows a play button in it’s place. In the Play button I have …

    countdown = (roundtime * 60000) + (getTimer() – pause);

    (I forget how I got that formula, but …)

    This works the first time I click play, the count_down display continues from where it left off, so I thought I’d figured it out. But, if you hit pause again, the next time you unpause it’s wrong.

    I’m having trouble getting my head around the getTimer counting up but the count_down is counting down, and what needs to be subtracted from what to get the count_down to start from where it left off.

    I feel like I’ve tried everything, so anyone who’s got an idea of how to get this workin’, I’d be interested in any ideas you have.

    Thanks

  50. Ter on June 9th, 2008 4:11 am

    Oh, the var in the pause button is called …
    pause
    which is in the formula in the play btn.

  51. Ter on June 10th, 2008 1:46 am

    Oh, and “roundtime” is input by the user in a previous screen and replaces the 7200000 in line 2 of the original script.

  52. Ter on June 11th, 2008 11:31 pm

    Obviously no one frequents this page … but, if you ever come across this and want to pause the countdown portion of Emanuele’s script, I’ve put this code into my play button and it seems to work …

    ============================================
    on (release) {
    start_time = getTimer() – elapsed_time + pause_time;
    var go = getTimer();
    pause_time = go – pause;
    countdown = (roundtime * 60000) + pause_time;
    count_down._visible = 1;
    paustime._visible = 0;
    }
    ============================================

    It took alot of guessing and I’m not sure exactly how I ended up with it or exactly why it works, but it does.

    If anyone wants it and can’t make out my method, I have no problem sending you my working version.

  53. eric on July 22nd, 2008 11:31 pm

    cant make it stop on “0″
    and go nextframe :(
    any of your suggestion worked for me
    i just have seconds
    its a countdown of 5 seconds and i want it to stop on 0
    and go next frame which say GAME OVER :(

  54. twyud on July 26th, 2008 5:41 am

    hey man thank’s for tut, i like your flash stuff

  55. Jeremy on August 13th, 2008 6:32 pm

    Thankyou very much for your code!

    I noticed that when the count finishes it starts again. So i added this to the end before the “return hours+”:” etc part.

    if (time_elapsed>=7200000) {
    gotoAndStop (2);
    }

    This makes sure that when the count reaches 0 it stops and does not repeat. Obviously you would have to change the gotoAndStop to whichever frame applies to you and the 7200000 changes depending on your certain time and should match the number on line 2 of the code.

    Thanks again,
    Jeremy

  56. arthur on August 31st, 2008 2:19 am

    Hi, I was wondering if anyone here knows how to restart the counter again, once it reaches 0.

  57. enache on September 4th, 2008 10:04 am

    hello
    please Ter can i have the code for you play\pause timer, i really really need him

  58. John on September 8th, 2008 7:53 pm

    hello, Can someone help me please? I want to load an external XML or TXT file and get from the external file a number to countdown = (external file number);

    thank you

  59. john on September 9th, 2008 4:27 pm

    Hello, i have a question…
    countdown = 7200000;<< this number”7200000″ how to load from external file?

  60. suj on October 2nd, 2008 3:31 pm

    Thanks so much! It would have taken me hours to figure this out ;)

  61. steve on October 22nd, 2008 4:27 pm

    Hi, Ter
    I am working on the pause btn aswell.
    Can you send you working version to vayphu1982@yahoo.com so I can play with my version.
    Thanks
    Steve

  62. Nick on October 27th, 2008 10:48 am

    Just wanted to say that Jeremy’s post works, just make it elapsed_time instead of time_elapsed. Thanks all.

  63. yogsmca on December 1st, 2008 12:51 pm

    Hi i need to setup for 5 min then i need to show susplay on the game that “you have lost already in game” after 5min over

  64. yogsmca on December 1st, 2008 12:52 pm

    Hi i need to setup for 5 min then i need to show Display on the game that “you have lost already in game” after 5min over

  65. George on December 14th, 2008 12:33 pm

    What about when somthing pops up how do i do that
    ad when you click somthing it stops

  66. yewyong on December 29th, 2008 7:19 pm

    Hi Ter, sorry to bother ya.
    Would you mind sending me your working version of the pause timer?
    My email is yew_yong@hotmail.com

    Thanks and happy new year.

  67. konnichi wa on January 27th, 2009 2:45 pm

    hello every one,
    how to pause the count down?

  68. t121hy on February 9th, 2009 8:14 am

    hello,
    how to make action if after times is over then will go to another frame.
    please send me tutorial via email
    i want make quiz for my student
    thanks
    trydantry@yahoo.co.id

  69. alex on March 29th, 2009 4:14 pm

    question, when i insert the time in my html and upload both to the web (they are in the same folder)
    i dont see the timer, please advice

  70. ccmqv on July 2nd, 2009 9:25 pm

    So do I have to add another frame so the time would stop.

    If so how do you add the second frame?

Leave a Reply




Trackbacks

  1. Flash game creation tutorial - part 5.3 at Emanuele Feronato on March 14th, 2007 12:51 pm

    [...] If you plan to make a racing game, the timer is very important. Some of the topics covered in this example are taken from the Flash simple timer/countdown tutorial. [...]

  2. Understanding AS3 Timer Class : Emanuele Feronato on November 18th, 2008 6:46 pm

    [...] lot of time ago I wrote about AS2 time management in Flash simple timer/countdown. Now it’s time to see how to manage time with [...]

Posts