MediaWiki:Gadget-countdown-timer.js: Difference between revisions
From IdleOn MMO Wiki
No edit summary |
(Converted to ES5) |
||
Line 1: | Line 1: | ||
$(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() { | function tick() { | ||
// Tick and automatically remove expired timers. | // Tick and automatically remove expired timers. | ||
timers = timers.filter( | timers = timers.filter(function(timer) { | ||
return tickTimer(timer); | |||
}); | |||
// Stop ticking if there are no timers remaining. | |||
if (timers.length != 0) { | if (timers.length != 0) { | ||
setTimeout(tick, 1000); | setTimeout(tick, 1000); | ||
Line 56: | Line 50: | ||
} | } | ||
mw.hook('wikipage.content').add($content | mw.hook('wikipage.content').add(function($content) { | ||
var $targets = $content.find('time.countdown-timer:not(.managed)'); | |||
if ($targets.length == 0) { | if ($targets.length == 0) { | ||
return; | return; | ||
Line 64: | Line 58: | ||
$targets | $targets | ||
.addClass('managed') | .addClass('managed') | ||
.each(() | .each(function() { | ||
console.log('FOUND TIMER!') | console.log('FOUND TIMER!'); | ||
timers.push({ | |||
$elem: $elem, | |||
expiration: Date.now() + (90 * 1000) | |||
}); | |||
}); | }); | ||
Revision as of 05:49, 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: $elem,
expiration: Date.now() + (90 * 1000)
});
});
tick();
});
});