Content added Content deleted
mNo edit summary |
m (automatically clear on finish) |
||
Line 47: | Line 47: | ||
if (diff.days <= 0 && diff.hours <= 0 && diff.minutes <= 0) { |
if (diff.days <= 0 && diff.hours <= 0 && diff.minutes <= 0) { |
||
countdownNode.html(finishedText || 'Countdown has ended'); |
countdownNode.html(finishedText || 'Countdown has ended'); |
||
finished = true; |
|||
} |
} |
||
Line 53: | Line 54: | ||
function init() { |
function init() { |
||
var intervals = []; |
|||
$('.js-countdown-wrapper2').each(function(idx) { |
$('.js-countdown-wrapper2').each(function(idx) { |
||
Line 70: | Line 69: | ||
console.log(idx + ' running'); |
console.log(idx + ' running'); |
||
}, 30000); |
}, 30000); |
||
intervals.push(interval); |
|||
}); |
}); |
||
} |
} |
Revision as of 22:35, 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');
finished = true;
}
return finished;
}
function init() {
$('.js-countdown-wrapper2').each(function(idx) {
var countdown = $(this).children('.js-countdown');
var d1 = new Date(countdown.data('countdown-end'));
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);
}
console.log(idx + ' running');
}, 30000);
});
}
$(document).ready(init);
})(jQuery);