Join the Webstudio community

Updated 2 months ago

countdown timer

At a glance
The community member has a countdown timer that works well, but it doesn't work when replicated on the same page. The comments suggest that the issue is because the script is only finding the first timer and adding it to that. To fix this, the community members recommend:

1. Giving each timer a unique ID and modifying the script to handle multiple timers.

2. Using data-attributes instead, which may be a cleaner solution. An example is provided that uses data-countdown and data-countdown-days, data-countdown-hours, etc. to handle multiple timers.

The community members indicate that this should resolve the issue of having multiple countdown timers on the same page.

H! i taked a cowntdown timer from templates and works well, but dont works if i replicate in the same page, can help me please to fix it?

<script>
//πŸ‘‡ Set the date we're counting down to
const countDownDate = new Date("Jan 13, 2026 21:00:00").getTime();
// πŸ‘†


// Update the count down every 1 second
const x = setInterval(function() {

// Get today's date and time
const now = new Date().getTime();

// Find the time left
const distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
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);

// Output the result in the elements with class="countdown-..."
document.querySelector('.countdown-day').innerHTML = days;
document.querySelector('.countdown-hr').innerHTML = hours;
document.querySelector('.countdown-min').innerHTML = minutes;
document.querySelector('.countdown-sec').innerHTML = seconds;
}, 1000);
</script>
Attachment
image.png
J
R
7 comments
@Ramvidal, quite a long time ago there was someone who built a countdown timer in here. His name was Alex. (I think he use to be a team member but is no longer). You might be able to find his file link somewhere if you search.
yes it is the file of alex
Ah. So you're including that same timer more than 1 time on the same page? You'll need to have multiple timers. Right now, it would only find the first one and add the timer to that.
  1. give each timer a unique ID
  2. modify script to handle more than 1
Try with data-attributes instead, it might be a cleaner solution... he's an example.

function initCountdownTimers() {
const containers = document.querySelectorAll('[data-countdown]');

containers.forEach(container => {
const targetDate = container.dataset.countdown;
const countDownDate = new Date(targetDate).getTime();
const interval = setInterval(() => {
const now = new Date().getTime();
const distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
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);

// Update the elements
const dayElement = container.querySelector('[data-countdown-days]');
const hourElement = container.querySelector('[data-countdown-hours]');
const minuteElement = container.querySelector('[data-countdown-minutes]');
const secondElement = container.querySelector('[data-countdown-seconds]');

if (dayElement) dayElement.textContent = days;
if (hourElement) hourElement.textContent = hours;
if (minuteElement) minuteElement.textContent = minutes;
if (secondElement) secondElement.textContent = seconds;

// If the countdown is finished, clear the interval
if (distance < 0) {
clearInterval(interval);
container.textContent = "EXPIRED";
}
}, 1000);
});
html structure:

<div data-countdown="Jan 13, 2026 21:00:00">
<span data-countdown-days></span> days
<span data-countdown-hours></span> hours
<span data-countdown-minutes></span> minutes
<span data-countdown-seconds></span> seconds
</div>

<div data-countdown="Dec 25, 2024 00:00:00">
<span data-countdown-days></span> days
<span data-countdown-hours></span> hours
<span data-countdown-minutes></span> minutes
<span data-countdown-seconds></span> seconds
</div>
Thank you I will test
Add a reply
Sign up and join the conversation on Discord
","url":"https://help.webstudio.is/countdown-timer-PWBF1GHgNLNx","identifier":"PWBF1GHgNLNx","publisher":{"@type":"Organization","name":"Webstudio","logo":{"@type":"ImageObject","url":"https://assets.usehall.com/org_01JEYTC8WWMF7N46W9J4J7CFDP/c7f10f06-f934-4e35-b008-431a89254f9f.png"}},"comment":[{"@type":"Comment","text":"@Ramvidal, quite a long time ago there was someone who built a countdown timer in here. His name was Alex. (I think he use to be a team member but is no longer). You might be able to find his file link somewhere if you search.","dateCreated":"2024-12-23T23:24:06.611Z","dateModified":"2024-12-23T23:24:06.611Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/6c66325d-fff8-43a2-b97e-549275813461","name":"Jeremy","identifier":"6c66325d-fff8-43a2-b97e-549275813461","image":"https://cdn.discordapp.com/avatars/383871071566430208/6de448190b31c7a9af49e5fbd4f5d047.webp?size=256"},"commentCount":0,"comment":[],"position":1,"upvoteCount":0},{"@type":"Comment","text":"yes it is the file of alex","dateCreated":"2024-12-23T23:40:35.562Z","dateModified":"2024-12-23T23:40:35.562Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/4238d172-c4ca-45c8-83b4-bd73feaace89","name":"Ramvidal","identifier":"4238d172-c4ca-45c8-83b4-bd73feaace89","image":"https://cdn.discordapp.com/avatars/889129940824506409/2c1657b3d88aef3f504bdc712a3845e7.webp?size=256"},"commentCount":0,"comment":[],"position":2,"upvoteCount":0},{"@type":"Comment","text":"Ah. So you're including that same timer more than 1 time on the same page? You'll need to have multiple timers. Right now, it would only find the first one and add the timer to that.","dateCreated":"2024-12-23T23:44:14.975Z","dateModified":"2024-12-23T23:44:14.975Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/6c66325d-fff8-43a2-b97e-549275813461","name":"Jeremy","identifier":"6c66325d-fff8-43a2-b97e-549275813461","image":"https://cdn.discordapp.com/avatars/383871071566430208/6de448190b31c7a9af49e5fbd4f5d047.webp?size=256"},"commentCount":0,"comment":[],"position":3,"upvoteCount":0},{"@type":"Comment","text":"give each timer a unique IDmodify script to handle more than 1","dateCreated":"2024-12-23T23:45:25.199Z","dateModified":"2024-12-23T23:45:25.199Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/6c66325d-fff8-43a2-b97e-549275813461","name":"Jeremy","identifier":"6c66325d-fff8-43a2-b97e-549275813461","image":"https://cdn.discordapp.com/avatars/383871071566430208/6de448190b31c7a9af49e5fbd4f5d047.webp?size=256"},"commentCount":0,"comment":[],"position":4,"upvoteCount":0},{"@type":"Comment","text":"Try with data-attributes instead, it might be a cleaner solution... he's an example.function initCountdownTimers() { const containers = document.querySelectorAll('[data-countdown]'); containers.forEach(container => { const targetDate = container.dataset.countdown; const countDownDate = new Date(targetDate).getTime(); const interval = setInterval(() => { const now = new Date().getTime(); const distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds 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); // Update the elements const dayElement = container.querySelector('[data-countdown-days]'); const hourElement = container.querySelector('[data-countdown-hours]'); const minuteElement = container.querySelector('[data-countdown-minutes]'); const secondElement = container.querySelector('[data-countdown-seconds]'); if (dayElement) dayElement.textContent = days; if (hourElement) hourElement.textContent = hours; if (minuteElement) minuteElement.textContent = minutes; if (secondElement) secondElement.textContent = seconds; // If the countdown is finished, clear the interval if (distance < 0) { clearInterval(interval); container.textContent = \"EXPIRED\"; } }, 1000); });","dateCreated":"2024-12-23T23:49:35.263Z","dateModified":"2024-12-23T23:49:35.263Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/6c66325d-fff8-43a2-b97e-549275813461","name":"Jeremy","identifier":"6c66325d-fff8-43a2-b97e-549275813461","image":"https://cdn.discordapp.com/avatars/383871071566430208/6de448190b31c7a9af49e5fbd4f5d047.webp?size=256"},"commentCount":0,"comment":[],"position":5,"upvoteCount":0},{"@type":"Comment","text":"html structure:
days hours minutes seconds
days hours minutes seconds
","dateCreated":"2024-12-23T23:49:55.730Z","dateModified":"2024-12-23T23:49:55.730Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/6c66325d-fff8-43a2-b97e-549275813461","name":"Jeremy","identifier":"6c66325d-fff8-43a2-b97e-549275813461","image":"https://cdn.discordapp.com/avatars/383871071566430208/6de448190b31c7a9af49e5fbd4f5d047.webp?size=256"},"commentCount":0,"comment":[],"position":6,"upvoteCount":0},{"@type":"Comment","text":"Thank you I will test","dateCreated":"2024-12-25T08:09:14.337Z","dateModified":"2024-12-25T08:09:14.337Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/4238d172-c4ca-45c8-83b4-bd73feaace89","name":"Ramvidal","identifier":"4238d172-c4ca-45c8-83b4-bd73feaace89","image":"https://cdn.discordapp.com/avatars/889129940824506409/2c1657b3d88aef3f504bdc712a3845e7.webp?size=256"},"commentCount":0,"comment":[],"position":7,"upvoteCount":0}],"author":{"@type":"Person","url":"https://help.webstudio.is/members/4238d172-c4ca-45c8-83b4-bd73feaace89","name":"Ramvidal","identifier":"4238d172-c4ca-45c8-83b4-bd73feaace89","image":"https://cdn.discordapp.com/avatars/889129940824506409/2c1657b3d88aef3f504bdc712a3845e7.webp?size=256"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0}}]