MediaWiki:Gadget-countdown-timer.js: Difference between revisions
From IdleOn MMO Wiki
(Converted to ES5) |
No edit summary |
||
Line 61: | Line 61: | ||
console.log('FOUND TIMER!'); | console.log('FOUND TIMER!'); | ||
timers.push({ | timers.push({ | ||
$elem: $ | $elem: $(this), | ||
expiration: Date.now() + (90 * 1000) | expiration: Date.now() + (90 * 1000) | ||
}); | }); |
Revision as of 05:50, 22 May 2024
$(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();
});
});