Home > General > Affiliate Marketing Forum

Javascript countdown script with milliseconds (4)


10-03-2011 09:50 AM #1 m0thm4n (Member)
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.

Code:
<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
                clearTimeout(timeoutval);
                // Do Something like close window or redirect page.
                alert('hola');
            }
            else {
                // if the milli seconds is below zero, de-increment(is that even a word??) the second and reset milliseconds
                if (milli < 0) {
                    seconds--;
                    milli = 1000;
                }
                else {
                   // Display the countdown
                    DisplayCountdown();
                }
                
                // 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;
            }

        }

        Timer();
	
	</script>
Here is some sample HTML code:

Code:
<p>This offer will close in <span id="timer"></span> seconds</p>
Happy to see people offer ways to make this code better!


10-04-2011 01:20 AM #2 liane (Member)

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.


10-04-2011 08:26 AM #3 m0thm4n (Member)

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).


01-29-2012 02:35 AM #4 bmw009 (Member)

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


Home > General > Affiliate Marketing Forum