MediaWiki:Gadget-countdown-timer.js: Difference between revisions

From IdleOn MMO Wiki
No edit summary
(Converted to ES5)
Line 1: Line 1:
/* jshint esversion: 6 */
$(function() {
 
$(() => {
let timers = [];
var timers = [];
class CountdownTimer {
function tickTimer(timer) {
constructor($elem) {
var time = Math.floor((timer.expiration - Date.now()) / 1000);
this.$elem = $elem;
// TODO Query the element for the expiration time.
var s = time % 60;
this.expiration = Date.now() + (90 * 1000);
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';
}
}
tick() {
showUnit |= s > 0;
const time = Math.floor((this.expiration - Date.now()) / 1000);
if (showUnit) {
text += s + 's';
const s = time % 60;
const m = Math.floor(time / 60) % 60;
const h = Math.floor(time / 3600) % 24;
const d = Math.floor(time / 86400);
let text = '';
let 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';
}
$elem.text(text);
return time > 0;
}
}
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(t => t.tick());
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) {
const $targets = $content.find('time.countdown-timer:not(.managed)');
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!');
const timer = new CountdownTimer($(this));
timers.push({
timers.push(timer);
$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();
	});
});