Countdown Timer Card
Build excitement and urgency for your next event or launch with this dynamic, animated countdown timer card.
This component is perfect for landing pages announcing a new product, a "coming soon" page, or a special promotion with a firm deadline. The timer is powered by lightweight, dependency-free JavaScript.
About this Component
This countdown timer is a self-contained unit that is easy to configure. The target date and time are set directly in the HTML using a data- attribute. The JavaScript then automatically calculates the remaining time and updates the numbers every second.
Features
- Easy Configuration: Simply set the target end date in the
data-target-dateHTML attribute. - Live, Ticking Clock: The timer updates every second to create a dynamic "live" effect.
- Graceful Completion: Once the countdown ends, the script automatically stops and can display a completion message.
- Clean Design: The timer is presented in large, clear number segments that are easy to read at a glance.
Code Breakdown
HTML Structure
The main card div holds a data-target-date attribute. This is where you set the target date in ISO 8601 format (e.g., "2026-01-01T00:00:00"). The timer itself is a container with four segments, one each for days, hours, minutes, and seconds. Each segment has a number span with a unique data-unit attribute (e.g., data-unit="days") and a label span.
JavaScript Logic
The script finds all countdown cards on the page. For each card, it reads the data-target-date attribute to get the target time. It then sets up an interval that runs every 1000 milliseconds (1 second).
Inside the interval function, it:
- Calculates the time difference between the current time and the target time.
- If the difference is less than zero, it stops the timer (using
clearInterval) and displays a completion message. - If time remains, it does the math to convert the total time difference into a number of days, hours, minutes, and seconds.
- It then finds the number
spanfor each unit (usingcard.querySelector('[data-unit="days"]')) and updates its text content. The numbers are padded with a leading zero to maintain a two-digit format.
Code
Here is the complete code. To use it, simply place the components in your project and update the data-target-date attribute in the HTML with your own event's date and time.