Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.