Gazelle/static/functions/class_countdown.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2011-03-28 14:21:28 +00:00
/*
TODO: Move to more efficient structure used in class_ajax.
Example Usage:
<script type="text/javascript">
window.onload = freeleech;
function freeleech() {
count.end = 'The freeleech has ended!';
count.event = 'The freeleech will end in';
count.element = 'freeleech';
count.update(<?=(time(3,0,0,9,22)-time());?>);
}
</script>
*/
"use strict";
var count = {
update: function (Offset) {
if (Offset < 0) {
document.getElementById(this.element).innerHTML = this.end;
} else {
var Seconds = Offset % 60;
if (Seconds < 10) {
Seconds = '0' + Seconds;
}
var Remainder = (Offset - Seconds) / 60;
var Minutes = Remainder % 60;
if (Minutes < 10) {
Minutes = '0' + Minutes;
}
Remainder = (Remainder - Minutes) / 60;
var Hours = Remainder % 24;
var Days = (Remainder - Hours) / 24;
$(this.element).raw().innerHTML = this.event + ' ' + (Days > 0 ? Days + 'd, ' : '') + (Hours > 0 ? Hours + 'h, ' : '') + Minutes + 'm, ' + Seconds + 's.';
setTimeout(function (object) {
object.update(Offset - 1);
}, 999, this);
}
}
};