More actions
Content deleted Content added
m use var instead of let |
mNo edit summary |
||
Line 2: | Line 2: | ||
function format(n, suffixes) { |
function format(n, suffixes) { |
||
const pr = new Intl.PluralRules('en-US'); |
const pr = new Intl.PluralRules('en-US'); |
||
return |
return n + ' ' + suffixes.get(pr.select(n)); |
||
} |
} |
||
Revision as of 22:09, 10 November 2021
(function ($) {
function format(n, suffixes) {
const pr = new Intl.PluralRules('en-US');
return n + ' ' + suffixes.get(pr.select(n));
}
function calcDiff(d1, d2) {
var 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 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'));
const finished = updateHTML(d1, countdown, countdown.data('end-text'));
if (finished) {
clearInterval(interval);
}
$(this).css({ 'display': 'block'});
var interval = setInterval(function() {
const finished = updateHTML(d1, countdown, countdown.data('end-text'));
if (finished) {
clearInterval(interval);
}
}, 30000);
intervals.push(interval);
});
}
$(document).ready(init);
}(jQuery));