Javascript countdown script with milliseconds

Hey guys, here is a countdown script I wrote that doesn’t use JQuery.

It’s probably no where near perfect because I suck at Maths AND javascript, but it works and you won’t have to include jquery on your landers.

<script type="text/javascript">
        // Set how many seconds to count down, milli stays at 1000
        var seconds = 59;
        var milli = 1000;
        var timeoutval;

        function Timer() {
            timeoutval = setTimeout(Timer, 10);

            if (seconds < 0) {
                // Stop the timeout function
                // Do Something like close window or redirect page.
            else {
                // if the milli seconds is below zero, de-increment(is that even a word??) the second and reset milliseconds
                if (milli < 0) {
                    milli = 1000;
                else {
                   // Display the countdown
                // de-increment milli by 10, 10 seemed to work the best
                milli = milli - 10;

        function DisplayCountdown() {
            // Fill a span tag with the id of timer
            var timer = document.getElementById('timer');
            // The code below pads the millisecond string so the value doesn't jump around
            if (milli < 10) {
                timer.innerHTML = seconds + '.00' + milli;
            else if (milli < 100) {
                timer.innerHTML = seconds + '.0' + milli;
            else {
                timer.innerHTML = seconds + '.' + milli;




Here is some sample HTML code:

<p>This offer will close in <span id="timer"></span> seconds</p>

Happy to see people offer ways to make this code better!

User Comment:
Oh, that’s just beautiful. You’d get 100 thanks from me if I could put them in there.

Thanks for putting clear comments at each step.

User Comment:
Thanks Liane!

I just made an update to the code (Added a way to shut off the ticks as it wasn’t letting the redirect happen).

User Comment:
Thanks so much~!! Ive been looking for this for awhile.

The Article Published IN 10-03-2011 09:50 AM

Share To More ()