Skip to content

Instantly share code, notes, and snippets.

@kilhage
Created June 22, 2011 22:16
Show Gist options
  • Save kilhage/1041394 to your computer and use it in GitHub Desktop.
Save kilhage/1041394 to your computer and use it in GitHub Desktop.
jQuery.clock
jQuery.fn.clock = (function () {
"use strict";
return function () {
return this.each(function (i, element) {
var d = 0, h = 0, m = 0, s = 0,
date = jQuery.trim(element.innerHTML), sd;
function update() {
var time = "";
if (d > 0) {
time += d + "d ";
}
if (h > 0) {
time += h + "h ";
}
if (m > 0) {
time += m + "m ";
}
if (s > 0) {
time += s + "s";
}
element.innerHTML = time;
}
if (!!date) {
sd = new Date() - new Date(date);
if (sd === sd) {
sd = new Date(new Date() - new Date(date));
m = sd.getMinutes();
h = sd.getHours() - 1;
s = sd.getSeconds();
}
}
update();
setInterval(function () {
if (++s === 60) {
s = 0;
if (++m === 60) {
m = 0;
if (++h === 24) {
h = 0;
if (++d === 30) {
d = 0;
}
}
}
}
update();
}, 1000);
});
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment