51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
/**
|
|
* Honest countdown timer — Подготовка к школе
|
|
*
|
|
* Reads deadline timestamp (seconds) from .honest-timer[data-deadline].
|
|
* Finds [data-timer] children within that timer container only.
|
|
* Updates (d, h, m, s) via requestAnimationFrame. Stops at 00:00:00:00.
|
|
*
|
|
* Поддерживает несколько таймеров на одной странице (cta-test + final-cta).
|
|
* Каждый экземпляр получает собственную замыкание.
|
|
*
|
|
* @package Dekart
|
|
*/
|
|
|
|
(function () {
|
|
var timers = document.querySelectorAll('.honest-timer');
|
|
if (!timers.length) return;
|
|
|
|
function pad(n) {
|
|
return n < 10 ? '0' + n : '' + n;
|
|
}
|
|
|
|
[].forEach.call(timers, function (timerEl) {
|
|
var deadline = parseInt(timerEl.getAttribute('data-deadline'), 10);
|
|
if (!deadline || deadline <= 0) return;
|
|
|
|
var deadlineMs = deadline * 1000;
|
|
var els = timerEl.querySelectorAll('[data-timer]');
|
|
if (!els.length) return;
|
|
|
|
var map = {};
|
|
[].forEach.call(els, function (el) {
|
|
map[el.getAttribute('data-timer')] = el;
|
|
});
|
|
|
|
function tick() {
|
|
var diff = deadlineMs - Date.now();
|
|
if (diff <= 0) {
|
|
for (var key in map) map[key].textContent = '00';
|
|
return;
|
|
}
|
|
map.d.textContent = pad(Math.floor(diff / 86400000));
|
|
map.h.textContent = pad(Math.floor((diff % 86400000) / 3600000));
|
|
map.m.textContent = pad(Math.floor((diff % 3600000) / 60000));
|
|
map.s.textContent = pad(Math.floor((diff % 60000) / 1000));
|
|
requestAnimationFrame(tick);
|
|
}
|
|
|
|
tick();
|
|
});
|
|
})();
|