+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How to create a countdown timer in Javascript

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    How to create a countdown timer in Javascript

    To create a countdown timer in JavaScript we will first need to make it tick with a interval of 1000 milliseconds (1 second). To do this we have to use the window.setTimeout function which will call a JavaScript function after a certain amount of time, like this:


    Code:
        window.setTimeout("Tick()", 1000);
    
        function Tick() {
    
        }
    So this will call the function called Tick after 1000 milliseconds, but each time Tick is called we want to set the timer to 1 second to make the timer continue so therefor it should look like this:


    Code:
        window.setTimeout("Tick()", 1000);
    
        function Tick() {
            window.setTimeout("Tick()", 1000);
        }
    So now we've made the base of the timer, the function Tick will be called once each second. But we want to be able to use it to change the text of something to show the time, so we can change the above code to this:


    Code:
    var Timer;
    var TotalSeconds;
    
    
    function CreateTimer(TimerID, Time) {
        Timer = document.getElementById(TimerID);
        TotalSeconds = Time;
        
        UpdateTimer()
        window.setTimeout("Tick()", 1000);
    }
    
    function Tick() {
        TotalSeconds -= 1;
        UpdateTimer()
        window.setTimeout("Tick()", 1000);
    }
    
    function UpdateTimer() {
        Timer.innerHTML = TotalSeconds;
    }
    So we have one function where we're creating the timer by getting an ID of the element we want to be a timer and the seconds it should tick down from which is stored in Timer and TotalSeconds, then we call the function UpdateTimer and then call the function Tick with a delay of 1 second. Each time the function Tick is called it lowers the amount of seconds by one and Calls UpdateTimer and set a timeout on it self on 1000 milliseconds. The function UpdateTimer will set the timers content to be the amount of seconds left.

    So the above example simply use one element to tick down once a second and display it in the element.

    The HTML for using it looks like this (if the above code is saved in /Timer.js):

    HTML Code:
    <html>
    <head>
    <script type="text/javascript" src="/Timer.js" />
    </head>
    
    <body>
    <div id='timer' />
    <script type="text/javascript">window.onload = CreateTimer("timer", 30);</script>
    </body>
    </html>
    The div above is our timer and the row below it will start the timer at 30 seconds.





    But there's two more things we should add, first thing is that our timer should stop at 0, not continuing to negative values as it do now, to fix this we'll change the Tick function to this:


    Code:
    function Tick() {
        if (TotalSeconds <= 0) {
            alert("Time's up!")
            return;
        }
    
        TotalSeconds -= 1;
        UpdateTimer()
        window.setTimeout("Tick()", 1000);
    }
    So if the timer has reached 0 (or just to be sure is below 0) we do what we want to happen when the time reaches 0 (in this case showing an alert box) and the use return to exit the function. By exiting the function here the timer will stop since we'll exit the function before the line with window.setTimeout.



    The other thing we need to add is the conversion from seconds to seconds, minutes, hours and maybe days since now it only show the amount of seconds left, to fix this we have to change the UpdateTimer function to something like this:



    Code:
    function UpdateTimer() {
        var Seconds = TotalSeconds;
        
        var Days = Math.floor(Seconds / 86400);
        Seconds -= Days * 86400;
    
        var Hours = Math.floor(Seconds / 3600);
        Seconds -= Hours * (3600);
    
        var Minutes = Math.floor(Seconds / 60);
        Seconds -= Minutes * (60);
    
    
        var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)
    
    
        Timer.innerHTML = TimeStr;
    }
    
    
    function LeadingZero(Time) {
    
        return (Time < 10) ? "0" + Time : + Time;
    
    }
    In the UpdateTimer function we now stores the TotalSeconds' value in a variable called Seconds and extracting the days, hours and minutes from it so there's only the seconds left. Then we store everything as a string called TimeStr, if the days is greater then 0 we adds them in the beginning. On the hours, minutes and seconds we're using a function called LeadinZero, it will return the hours, minutes or seconds with a leading 0 if they only had one number before. This will make the output something like this:

    Code:
    00:02:26
    Instead of this:

    Code:
    0:2:26
    Then we just set the Timer's text to be the value of TimeStr. If you want you can replace this line:

    Code:
        Timer.innerHTML = TimeStr;
    with something like this:

    Code:
        Timer.innerHTML = TimeStr + " until my birthday!";

    This was everything about "How to create a countdown timer in Javascript". Hope you'll find this useful.
    Last edited by Vswe; 04-04-2010 at 03:07 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: How to create a countdown timer in Javascript

    Nicely done! Did you use this in the CC game?

    +rep

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: How to create a countdown timer in Javascript

    Quote Originally Posted by Jordan View Post
    Nicely done! Did you use this in the CC game?

    +rep
    Yes I did

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: How to create a countdown timer in Javascript

    Very nice!

  6. #5
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: How to create a countdown timer in Javascript

    nice work VSWE
    ++++Rep
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  7. #6
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: How to create a countdown timer in Javascript

    Simple and useful +rep

  8. #7
    Join Date
    Dec 2009
    Location
    somewhere over the rainbow
    Posts
    3
    Blog Entries
    2
    Rep Power
    0

    Re: How to create a countdown timer in Javascript

    Great Ideas from a great minds

    hahah greatly done..please pay me a visit on my forum to discuss some brainstorming

  9. #8
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: How to create a countdown timer in Javascript

    Great One .. +rep

  10. #9
    codesc.ws's Avatar
    codesc.ws is offline Newbie
    Join Date
    Apr 2010
    Location
    UK, North west
    Posts
    3
    Rep Power
    0

    Re: How to create a countdown timer in Javascript

    Thanks for this. Helped a lot!

  11. #10
    nadee158 is offline Newbie
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Re: How to create a countdown timer in Javascript

    Thanks a lot for this. Great work!

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 6 users browsing this thread. (0 members and 6 guests)

Similar Threads

  1. C# and WPF countdown timer
    By Jrb in forum C# Programming
    Replies: 1
    Last Post: 10-11-2011, 12:47 PM
  2. Need help with Countdown timer
    By Myvision in forum JavaScript and CSS
    Replies: 3
    Last Post: 04-29-2011, 10:31 AM
  3. javascript countdown timer
    By theonebeyond in forum JavaScript and CSS
    Replies: 2
    Last Post: 02-07-2011, 07:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts