| 1 |
(function ($) { |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
window.initCountdown = function($scope, $) { |
| 5 |
// Use $scope to find element inside editor iframe |
| 6 |
const countdownWrapper = $scope.find('#eel-countdowns')[0] || document.getElementById('eel-countdowns'); |
| 7 |
if (!countdownWrapper) return; |
| 8 |
|
| 9 |
const targetDateStr = countdownWrapper.dataset.target; |
| 10 |
const countDownDate = new Date(targetDateStr).getTime(); |
| 11 |
if (isNaN(countDownDate)) return; |
| 12 |
|
| 13 |
const second = 1000, |
| 14 |
minute = second * 60, |
| 15 |
hour = minute * 60, |
| 16 |
day = hour * 24; |
| 17 |
|
| 18 |
const daysEl = countdownWrapper.querySelector('.eel-cntdwn-days'); |
| 19 |
const hoursEl = countdownWrapper.querySelector('.eel-cntdwn-hours'); |
| 20 |
const minutesEl = countdownWrapper.querySelector('.eel-cntdwn-minutes'); |
| 21 |
const secondsEl = countdownWrapper.querySelector('.eel-cntdwn-seconds'); |
| 22 |
|
| 23 |
if (!daysEl || !hoursEl || !minutesEl || !secondsEl) return; |
| 24 |
|
| 25 |
function updateCountdown() { |
| 26 |
const now = new Date().getTime(); |
| 27 |
const distance = countDownDate - now; |
| 28 |
|
| 29 |
if (distance <= 0) { |
| 30 |
countdownWrapper.innerHTML = "Countdown Finished!"; |
| 31 |
clearInterval(intervalId); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
daysEl.textContent = Math.floor(distance / day); |
| 36 |
hoursEl.textContent = Math.floor((distance % day) / hour); |
| 37 |
minutesEl.textContent = Math.floor((distance % hour) / minute); |
| 38 |
secondsEl.textContent = Math.floor((distance % minute) / second); |
| 39 |
} |
| 40 |
|
| 41 |
updateCountdown(); |
| 42 |
const intervalId = setInterval(updateCountdown, 1000); |
| 43 |
}; |
| 44 |
|
| 45 |
// Elementor hook (editor + frontend) |
| 46 |
jQuery(window).on('elementor/frontend/init', function () { |
| 47 |
elementorFrontend.hooks.addAction('frontend/element_ready/global', function($scope, $){ |
| 48 |
if (typeof window.initCountdown === 'function') { |
| 49 |
window.initCountdown($scope, $); |
| 50 |
} |
| 51 |
}); |
| 52 |
}); |
| 53 |
|
| 54 |
})(jQuery); |
| 55 |
|