class CustomCountdown extends HTMLElement { constructor() { super(); this.date = this.getAttribute('date'); } connectedCallback() { this.attachShadow({ mode: 'open' }); this.render(); this.startCountdown(); } render() { this.shadowRoot.innerHTML = `
00
Days
00
Hours
00
Minutes
00
Seconds
`; } startCountdown() { const countdownDate = new Date(this.date).getTime(); const updateCountdown = () => { const now = new Date().getTime(); const distance = countdownDate - now; if (distance < 0) { this.shadowRoot.querySelector('#days').textContent = '00'; this.shadowRoot.querySelector('#hours').textContent = '00'; this.shadowRoot.querySelector('#minutes').textContent = '00'; this.shadowRoot.querySelector('#seconds').textContent = '00'; return; } const days = Math.floor(distance / (1000 * 60 * 60 * 24)); const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((distance % (1000 * 60)) / 1000); this.shadowRoot.querySelector('#days').textContent = days.toString().padStart(2, '0'); this.shadowRoot.querySelector('#hours').textContent = hours.toString().padStart(2, '0'); this.shadowRoot.querySelector('#minutes').textContent = minutes.toString().padStart(2, '0'); this.shadowRoot.querySelector('#seconds').textContent = seconds.toString().padStart(2, '0'); }; updateCountdown(); setInterval(updateCountdown, 1000); } } customElements.define('custom-countdown', CustomCountdown);