Skip to content

Instantly share code, notes, and snippets.

@MatrixFrog
Created September 18, 2011 19:37
Show Gist options
  • Save MatrixFrog/1225459 to your computer and use it in GitHub Desktop.
Save MatrixFrog/1225459 to your computer and use it in GitHub Desktop.
Countdown clock for psychogate.com
$(function() {
function getCurrentTime() {
var now = new Date();
var nowInUTC = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var nowInPacific = new Date(nowInUTC - 7*60*60*1000);
return nowInPacific;
}
function getNextStartTimeAfter(time) {
var startTimes = [
new Date(2011, 9, 1, 6, 0, 0, 0),
new Date(2011, 9, 29, 6, 0, 0, 0),
new Date(2011, 9, 1, 6, 0, 0, 0),
new Date(2011, 10, 12, 6, 0, 0, 0),
new Date(2011, 10, 26, 6, 0, 0, 0)
];
for (var i in startTimes) {
startTime = startTimes[i];
if (startTime > time) {
return startTime;
}
}
}
function startCountdown() {
var now = getCurrentTime();
var timeUntilNextPsychogate = getNextStartTimeAfter(now) - now;
setInterval(function() {
displayTime(timeUntilNextPsychogate);
timeUntilNextPsychogate -= 1000;
}, 1000);
}
var countdownDiv = $('#countdown');
var secondInMillis = 1000;
var minuteInMillis = 60*secondInMillis;
var hourInMillis = 60*minuteInMillis;
var dayInMillis = 24*hourInMillis;
function displayTime(time) {
var days = Math.floor(time / dayInMillis);
time -= days*dayInMillis;
var hours = Math.floor(time / hourInMillis);
time -= hours*hourInMillis;
var minutes = Math.floor(time / minuteInMillis);
time -= minutes*minuteInMillis;
var seconds = Math.floor(time / secondInMillis);
countdownDiv.text(
days + ':' +
zeroPad(hours) + ':' +
zeroPad(minutes) + ':' +
zeroPad(seconds));
}
function zeroPad(x) {
return (x.toString().length < 2) ? '0'+x : x
}
startCountdown();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment