19 lines
701 B
JavaScript
19 lines
701 B
JavaScript
function updateCountdowns() {
|
|
const now = Math.floor(Date.now() / 1000);
|
|
for (const clock of document.querySelectorAll('[data-be-countdown]')) {
|
|
const remaining = Number(clock.dataset.beCountdown) - now;
|
|
if (remaining <= 0) {
|
|
clock.textContent = clock.dataset.beStarted;
|
|
continue;
|
|
}
|
|
const days = Math.floor(remaining / 86400);
|
|
const hours = Math.floor(remaining / 3600) % 24;
|
|
const minutes = Math.floor(remaining / 60) % 60;
|
|
const seconds = remaining % 60;
|
|
const time = [hours, minutes, seconds].map((part) => String(part).padStart(2, '0')).join(':');
|
|
clock.textContent = days ? `${days}d ${time}` : time;
|
|
}
|
|
}
|
|
updateCountdowns();
|
|
window.setInterval(updateCountdowns, 1000);
|