1const dateCountdownEls = document.querySelectorAll('.date-countdown'); 2console.log('dateCountdownEls', dateCountdownEls); 3dateCountdownEls.forEach(function(obj) { 4 const date = new Date(obj.dataset.date).getTime(); 5 console.log('date', date); 6 7 8 9// Function to find the closest preceding element with the given selector 10function findClosestPreviousElement(element, selector) { 11 let previousElement = element.previousElementSibling; 12 while (previousElement) { 13 if (previousElement.matches(selector)) { 14 return previousElement; 15 } 16 previousElement = previousElement.previousElementSibling; 17 } 18 19 // If not found in siblings, check parent and parent's previous siblings 20 let parentElement = element.parentElement; 21 while (parentElement) { 22 previousElement = parentElement.previousElementSibling; 23 while (previousElement) { 24 const found = previousElement.querySelector(selector); 25 if (found) { 26 return found; 27 } 28 previousElement = previousElement.previousElementSibling; 29 } 30 parentElement = parentElement.parentElement; 31 } 32 return null; 33} 34 35// Get today's date and time 36const now = new Date().getTime(); 37 38// Find the distance between now and the count down date 39const distance = date - now; 40 41// Time calculations for days, hours, minutes and seconds 42const days = Math.floor(distance / (1000 * 60 * 60 * 24)); 43const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); 44const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); 45const seconds = Math.floor((distance % (1000 * 60)) / 1000); 46 47// Check if any of the calculated values is NaN 48if (isNaN(days) || isNaN(hours) || isNaN(minutes) || isNaN(seconds)) { 49 // Find the closest preceding element with class 'opening-text' 50 const closestOpeningText = findClosestPreviousElement(obj, 'p.opening-text'); 51 52 console.log("closestOpeningText: " + closestOpeningText); 53 54 if (closestOpeningText) { 55 obj.innerHTML = closestOpeningText.textContent; 56 } else { 57 obj.innerHTML = ''; // Fallback if no such element is found 58 } 59} else { 60 // Display the result in the element with id="demo" 61 obj.innerHTML = '<span class="s"><span>' + days + '</span><span class="label">Days</span></span> <span class="s"><span>' + hours + '</span><span class="label">Hours</span></span> <span class="s"><span>' 62 + minutes + '</span><span class="label">Minutes</span></span> '; 63} 64 65 // If the count down is finished, write some text 66 if (distance < 0) { 67 clearInterval(x); 68 obj.innerHTML = "EXPIRED"; 69 } 70 71});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.