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

MediaWiki:Countdown.js: Difference between revisions

MediaWiki interface page
Content deleted Content added
ChaoticShadow (talk | contribs)
mNo edit summary
ChaoticShadow (talk | contribs)
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 11: Line 11:
}
}
const intervals = [];
const daySuffixes = new Map([
['one', 'day'],
['other', 'days'],
]);
const hourSuffixes = new Map([
['one', 'hour'],
['other', 'hours']
]);
const minuteSuffixes = new Map([
['one', 'minute'],
['other', 'minutes']
]);
function format(n, suffixes) {
const pr = new Intl.PluralRules('en-US');
return `${n} ${suffixes.get(pr.select(n))}`;
}
var intervals = [];
function updateHTML(targetDate, countdownNode, index) {
function updateHTML(targetDate, countdownNode, index) {
var diff = calcDiff(new Date(), targetDate);
var diff = calcDiff(new Date(), targetDate);
const pr = new Intl.PluralRules('en-US');
const daySuffixes = new Map([
['one', 'day'],
['other', 'days'],
]);
const hourSuffixes = new Map([
['one', 'hour'],
['other', 'hours']
]);
const minuteSuffixes = new Map([
['one', 'minute'],
['other', 'minutes']
])
countdownNode.html(
countdownNode.html(
`
diff.days + ' days, ' +
diff.hours + ' hours, ' +
${format(diff.days, daySuffixes)},
${format(diff.hours, hourSuffixes)},
diff.minutes + ' ' + minuteSuffixes.get(pr.select(diff.minutes))
${format(diff.minutes, minuteSuffixes)}
`
);
);
if (diff.days <= 0 && diff.hours <= 0 && diff.minutes <= 0) {
if (diff.days <= 0 && diff.hours <= 0 && diff.minutes <= 0) {
intervals[index] = clearInterval(intervals[index]);
intervals[index] = clearInterval(intervals[index]);
countdownNode.html('Event ended');
countdownNode.html('Event has ended');
}
}
}
}

Latest revision as of 22:26, 18 July 2021

$(function() {
	function calcDiff(d1, d2) {
		let remDiff = d2 - d1;
		const days = Math.floor(remDiff / (1000*60*60*24));
		remDiff = remDiff % (1000*60*60*24);
		const hours = Math.floor(remDiff / (1000*60*60));
		remDiff = remDiff % (1000*60*60);
		const minutes = Math.floor(remDiff / (1000*60));
		
		return { days: days, hours: hours, minutes: minutes };
	}
	
	
	const daySuffixes = new Map([
		['one',   'day'],
		['other', 'days'],
	]);
	const hourSuffixes = new Map([
		['one', 'hour'],
		['other', 'hours']
	]);
	const minuteSuffixes = new Map([
		['one', 'minute'],
		['other', 'minutes']
	]);
	
	function format(n, suffixes) {
		const pr = new Intl.PluralRules('en-US');
		return `${n} ${suffixes.get(pr.select(n))}`;
	}
	
	var intervals = [];
	
	function updateHTML(targetDate, countdownNode, index) {
		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) {
			intervals[index] = clearInterval(intervals[index]);
			countdownNode.html('Event has ended');
		}
	}
	
	$('.js-countdown-wrapper').each(function(idx) {
		
		var countdown = $(this).children('.js-countdown');
		var d1 = new Date(countdown.data('event-end'));
		
		updateHTML(d1, countdown, idx);
		$(this).css({ 'display': 'block'});
		
		var interval = setInterval(function() {
			updateHTML(d1, countdown, idx);
		}, 30000);
		intervals.push(interval);
	});

}());
Cookies help us deliver our services. By using our services, you agree to our use of cookies.