Pages

Tuesday, May 21, 2019

Countdown timer JS code


<section class="black-bar">
    <h4>Cash Game Fix enrollment ends in:</h4>
    <h2 class="countdown"><span class="days"></span> days <span class="line">|</span> <span class="hours"></span> hours <span class="line">|</span> <span class="minutes"></span> min <span class="line">|</span> <span class="seconds"></span> sec</h2>
</section>
<script type="text/javascript">
    /* STatic Date Timer start */

    function makeTimer() {

            // var endTime = new Date("29 April 2018 9:56:00 GMT+01:00"); 
            var endTime = new Date("24 May 2019 23:59:59 GMT-07:00");         
            endTime = (Date.parse(endTime) / 1000);

            var now = new Date();
            now = (Date.parse(now) / 1000);


            var timeLeft = endTime - now;


            var days = Math.floor(timeLeft / 86400);
            var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
            var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600 )) / 60);
            var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));


            $clock = $('h2.countdown');
            $days = $clock.find('.days');
            $hours = $clock.find('.hours');
            $minutes = $clock.find('.minutes');
            $seconds = $clock.find('.seconds');

            if(days < 0) days = 0;

            if(hours < 0) hours = 0;

            if(minutes < 0) minutes = 0;

            if(seconds < 0) seconds = 0;

            // If lower than 10, then add 0;
            if( days < 10 ) $days.html('0' + days);
            if( hours < 10 ) $hours.html('0' + hours);
            if( minutes < 10 ) $minutes.html('0' + minutes);
            if( seconds < 10 ) $seconds.html('0' + seconds);

            // If higher than don't add anything.
            if( days > 10 ) $days.html(days);
            if( hours > 10 ) $hours.html(hours);
            if( minutes > 10 ) $minutes.html(minutes);
            if( seconds > 10 ) $seconds.html(seconds);

            if (timeLeft <= 0) {
                clearInterval(makeTimer());
            }

    }
    $(document).ready(function(){
        setInterval(function() { makeTimer(); }, 1000);   
    })
   
</script>