MediaWiki:Gadget-countdown-timer.js
From IdleOn MMO Wiki
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
$(function() {
var timers = [];
function tickTimer(timer) {
var time = Math.floor((timer.expiration - Date.now()) / 1000);
var s = time % 60;
var m = Math.floor(time / 60) % 60;
var h = Math.floor(time / 3600) % 24;
var d = Math.floor(time / 86400);
var text = '';
var showUnit = d > 0;
if (showUnit) {
text += d + 'd';
}
showUnit |= h > 0;
if (showUnit) {
text += h + 'h';
}
showUnit |= m > 0;
if (showUnit) {
text += m + 'm';
}
showUnit |= s > 0;
if (showUnit) {
text += s + 's';
}
timer.$elem.text(text);
return time > 0;
}
function tick() {
// Tick and automatically remove expired timers.
timers = timers.filter(function(timer) {
return tickTimer(timer);
});
// Stop ticking if there are no timers remaining.
if (timers.length != 0) {
setTimeout(tick, 1000);
}
}
mw.hook('wikipage.content').add(function($content) {
var $targets = $content.find('time.countdown-timer:not(.managed)');
if ($targets.length == 0) {
return;
}
$targets
.addClass('managed')
.each(function() {
console.log('FOUND TIMER!');
timers.push({
$elem: $(this),
expiration: Date.now() + (90 * 1000)
});
});
tick();
});
});