Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Gadget-countdown.js: Difference between revisions

MediaWiki interface page
Content added Content deleted
m (replace map with object)
m (replace let and const with var)
Line 1: Line 1:
(function ($) {
(function ($) {
function format(n, suffixes) {
function format(n, suffixes) {
const pr = new Intl.PluralRules('en-US');
var pr = new Intl.PluralRules('en-US');
return n + ' ' + suffixes[pr.select(n)];
return n + ' ' + suffixes[pr.select(n)];
}
}
function calcDiff(d1, d2) {
function calcDiff(d1, d2) {
let remDiff = d2 - d1;
var remDiff = d2 - d1;
const days = Math.floor(remDiff / (1000*60*60*24));
var days = Math.floor(remDiff / (1000*60*60*24));
remDiff = remDiff % (1000*60*60*24);
remDiff = remDiff % (1000*60*60*24);
const hours = Math.floor(remDiff / (1000*60*60));
var hours = Math.floor(remDiff / (1000*60*60));
remDiff = remDiff % (1000*60*60);
remDiff = remDiff % (1000*60*60);
const minutes = Math.floor(remDiff / (1000*60));
var minutes = Math.floor(remDiff / (1000*60));
return {
return {
Line 20: Line 20:
}
}
const daySuffixes = {
var daySuffixes = {
one: 'day',
one: 'day',
other: 'days',
other: 'days',
};
};
const hourSuffixes = {
var hourSuffixes = {
one: 'hour',
one: 'hour',
other: 'hours'
other: 'hours'
};
};
const minuteSuffixes = {
var minuteSuffixes = {
one: 'minute',
one: 'minute',
other: 'minutes'
other: 'minutes'
Line 34: Line 34:
function updateHTML(targetDate, countdownNode, finishedText) {
function updateHTML(targetDate, countdownNode, finishedText) {
let finished = false;
var finished = false;
let diff = calcDiff(new Date(), targetDate);
var diff = calcDiff(new Date(), targetDate);
countdownNode.html(
countdownNode.html(
Line 53: Line 53:
function init() {
function init() {
let intervals = [];
var intervals = [];
$('.js-countdown-wrapper2').each(function(idx) {
$('.js-countdown-wrapper2').each(function(idx) {
let countdown = $(this).children('.js-countdown');
var countdown = $(this).children('.js-countdown');
let d1 = new Date(countdown.data('countdown-end'));
var d1 = new Date(countdown.data('countdown-end'));
const finished = updateHTML(d1, countdown, countdown.data('end-text'));
var finished = updateHTML(d1, countdown, countdown.data('end-text'));
$(this).css({ 'display': 'block'});
$(this).css({ 'display': 'block'});
let interval = setInterval(function() {
var interval = setInterval(function() {
const finished = updateHTML(d1, countdown, countdown.data('end-text'));
var finished = updateHTML(d1, countdown, countdown.data('end-text'));
if (finished) {
if (finished) {
clearInterval(interval);
clearInterval(interval);

Revision as of 22:18, 10 November 2021

(function ($) {
	function format(n, suffixes) {
		var pr = new Intl.PluralRules('en-US');
		return n + ' ' + suffixes[pr.select(n)];
	}
	
	function calcDiff(d1, d2) {
		var remDiff = d2 - d1;
		var days = Math.floor(remDiff / (1000*60*60*24));
		remDiff = remDiff % (1000*60*60*24);
		var hours = Math.floor(remDiff / (1000*60*60));
		remDiff = remDiff % (1000*60*60);
		var minutes = Math.floor(remDiff / (1000*60));
		
		return {
			days: days,
			hours: hours,
			minutes: minutes
		};
	}
	
	var daySuffixes = {
		one: 'day',
		other: 'days',
	};
	var hourSuffixes = {
		one: 'hour',
		other: 'hours'
	};
	var minuteSuffixes = {
		one: 'minute',
		other: 'minutes'
	};
	
	function updateHTML(targetDate, countdownNode, finishedText) {
		var finished = false;
		var diff = calcDiff(new Date(), targetDate);
		
		countdownNode.html(
			format(diff.days, daySuffixes) + 
			', ' +
			format(diff.hours, hourSuffixes) +
			', ' +
			format(diff.minutes, minuteSuffixes)
		);
		
		if (diff.days <= 0 && diff.hours <= 0 && diff.minutes <= 0) {
			countdownNode.html(finishedText || 'Countdown has ended');
		}
		
		return finished;
	}
	
	function init() {
		var intervals = [];
		
		$('.js-countdown-wrapper2').each(function(idx) {
			
			var countdown = $(this).children('.js-countdown');
			var d1 = new Date(countdown.data('countdown-end'));
			
			var finished = updateHTML(d1, countdown, countdown.data('end-text'));
			$(this).css({ 'display': 'block'});
			
			var interval = setInterval(function() {
				var finished = updateHTML(d1, countdown, countdown.data('end-text'));
				if (finished) {
					clearInterval(interval);
				}
			}, 30000);
			intervals.push(interval);
		});
	}
	
	$(document).ready(init);
}(jQuery));
Cookies help us deliver our services. By using our services, you agree to our use of cookies.