Skip to content

Instantly share code, notes, and snippets.

@zyphlar
Created February 22, 2014 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zyphlar/9161989 to your computer and use it in GitHub Desktop.
Save zyphlar/9161989 to your computer and use it in GitHub Desktop.
HTML/Javascript Countdown timer
<script type="text/javascript">// <![CDATA[
/* Javascript Countdown Timer
* Will Bradley (www.willbradley.name) Dec. 2009
* Code released publicly without any warranty or license.
*/
function showtime() {
dateString = new Date().toUTCString();
now = Date.parse(dateString);
xmasDateString = new Date(1261724400000).toUTCString(); // The long number is December 25, 2009 in Javascript's .getTime() format. You can put almost any value in here, including plain english dates.
xmasDate = Date.parse(xmasDateString);
dd = xmasDate - now;
dday=Math.floor(dd/(60*60*1000*24)*1);
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
if(now > xmasDate)
{
// Put any "countdown done!" code here. I've got it reloading in order to pull new info via a coordinated PHP countdown, but you could put anything.
window.location.reload();
document.getElementById("secs").innerHTML = "Reload the page for your gift!";
}
else{
document.getElementById("secs").innerHTML =
dday + "<em>days</em>" +
dhour + "<em>hours</em>" +
dmin + "<em>minutes</em>" +
dsec + "<em>seconds</em>";
timerID = setTimeout("showtime()",1000);
}
}
// ]]></script>
<style type="text/css"><!--
body { background-color: black; color: white; text-align: center;
font-family: Lucida Sans Unicode, Lucida Grande, sans-serif; font-size: 12px;
}
h1 { color: #ee0000; font-weight: bold; margin-top: 150px; font-size: 4em; text-transform: uppercase; }
h1 em { color: white; font-style: normal; margin: 0 0.5em 0 0.25em; font-size: 0.4em; }
--></style>
<h1 id="secs"></h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment