Monday, November 24, 2014

Javascript Countdown

Heres some code for a countdown that I'm using for my Missionary blog. I stole most/all of the code from Mindgrader, and only made a few changes (like only showing days and adding the whole days till I report to the MTC). You can use this in a blogger sidebar gadget by just adding a html/javascript gadget.
<h1><span id="countdown"></span></h1>Days till I report to the MTC
<script>
var target_date = new Date("Aug 15, 2015").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);

    // format countdown string + set tag value
    countdown.innerHTML = days ; 
}, 1000);

</script>
It should look like this

Days till I report to the MTC

No comments:

Post a Comment