Skip to content

Niyander Tech

Learn with fun

Menu
  • Home
  • Categories
    • Android
    • Alpine.js Components
    • Genshin Impact News
    • Jobs and Internship
    • Coursera Hub
  • Our Policies
    • Privacy Policy
    • Terms and Conditions
    • Contact Us
  • Coursera Search Engine
  • AI Tools
    • AI Background Remover
    • AI Video Downloader
    • 98+ Beautiful CSS Box-Shadow
    • G-Drive Download Link Generator
Menu
Countdown Timer

How to Create a Countdown Timer with Alpine.js

Posted on April 2, 2025April 2, 2025 by Niyander

In this article, I will explain how you can create a Countdown Timer using Alpine.js. If you’re not familiar with Alpine.js, don’t worry—I will first give you a brief introduction before showing you how to build the timer.

So What’s Alpine.js, Anyway?

Okay, so Alpine.js is this neat little JavaScript framework. It’s super lightweight, doesn’t take up much space, and lets you add cool interactive features to your webpage without breaking a sweat. It’s kind of like Vue.js, but way simpler—no complex setup or anything.

If you’re like me and just want to throw some quick JavaScript into your HTML without dragging in React, Vue, or whatever else, this is the way to go.

If you want to learn more about Alpine.js, just read my full article Introduction to Alpine.js

Here’s what I like about Alpine.js:

  1. Really tiny file size—less than 10KB.
  2. Super easy to learn and use, even for the simplest things!
  3. Feels a bit like Vue.js, if you’re familiar with that.
  4. It is lightweight and doesn’t take up much space.”

Countdown Timer Preview


Now, let’s dive into the implementation. I’ll walk you through each step, making it simple and easy to follow so you can get your countdown timer up and running in no time!

Step by Step guide for you

Step 1: Basic HTML

Create a basic HTML page.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Countdown Timer</title>
  </head>
  <body>
    <!-- code goes here -->
  </body>
</html>

Step 2: Add Alpine.js

Add the Alpine.js CDN link to your file.

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js" defer></script>

Step 3: Set Up Countdown

  • Define the countdown data, including the target date and time.
<div x-data="{
  targetTime: new Date().getTime() + (80 * 24 * 60 * 60 + 15 * 60 * 60 + 53 * 60 + 41) * 1000,
  timeLeft: {}
}">

Step 4: Update Time

Create a function that calculates the time left and updates the countdown.”

updateCountdown() {
  let difference = this.targetTime - new Date().getTime();
  this.timeLeft = difference > 0 ? {
    days: Math.floor(difference / (1000 * 60 * 60 * 24)),
    hours: Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
    minutes: Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)),
    seconds: Math.floor((difference % (1000 * 60)) / 1000)
  } : { days: 0, hours: 0, minutes: 0, seconds: 0 };
},

Step 5: Run It

Start the countdown and set it to repeat until the target time is reached.

init() {
  this.updateCountdown();
  setInterval(() => this.updateCountdown(), 1000);
}

Step 6: Show Time

Display the countdown on the page by binding the calculated time to an HTML element.”

<div>
  <h1>Countdown to Launch</h1>
  <div>
    <span x-text="timeLeft.days"></span> Days
    <span x-text="timeLeft.hours"></span> Hours
    <span x-text="timeLeft.minutes"></span> Minutes
    <span x-text="timeLeft.seconds"></span> Seconds
  </div>
</div>

Full Code Snippet

Here’s everything together:

Here’s a simple countdown timer made with Alpine.js. It doesn’t have any CSS or Tailwind, but you can copy or download it and style it with Tailwind or any other design you like!

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Countdown Timer</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js" defer></script>
  </head>
  <body>
    <div x-data="{
      targetTime: new Date().getTime() + (80 * 24 * 60 * 60 + 15 * 60 * 60 + 53 * 60 + 41) * 1000,
      timeLeft: {},
      updateCountdown() {
        let difference = this.targetTime - new Date().getTime();
        this.timeLeft = difference > 0 ? {
          days: Math.floor(difference / (1000 * 60 * 60 * 24)),
          hours: Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
          minutes: Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)),
          seconds: Math.floor((difference % (1000 * 60)) / 1000)
        } : { days: 0, hours: 0, minutes: 0, seconds: 0 };
      },
      init() {
        this.updateCountdown();
        setInterval(() => this.updateCountdown(), 1000);
      }
    }">
      <div>
        <h1>Countdown to Launch</h1>
        <div>
          <span x-text="timeLeft.days"></span> Days
          <span x-text="timeLeft.hours"></span> Hours
          <span x-text="timeLeft.minutes"></span> Minutes
          <span x-text="timeLeft.seconds"></span> Seconds
        </div>
      </div>
    </div>
  </body>
</html>

View Demo
Download Now

Final Thoughts

This code creates a simple countdown timer. The HTML builds the page, Alpine.js powers the timer, and the logic sets a future time, updates it every second, and shows the days, hours, minutes, and seconds left. It’s an easy way to make a live countdown for any event!

Category: Alpine.js Components

Post navigation

← Introduction to Alpine.js: A Lightweight JavaScript Framework
Creating a Dynamic Dropdown Menu using Alpine.js →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Notice

Hey guys! Need a Google Drive Direct Download Link Generator? I made a simple tool that does just that. 🚀
Convert any shared Drive link into a direct download link instantly.
Try it out now: https://niyander.com/projects/tools/gdrive/
Let me know your thoughts!

Notice

Hi everyone! I've built a collection of 98+ CSS Box-Shadow Examples for developers and designers! 🎨
Click on any card to copy the shadow — super handy for your projects.
Explore them all here: https://niyander.com/projects/tools/box-shadow/
Hope you find it useful!

Notice

Hey folks! Excited to share my new tool — an All-in-One Social Media Downloader! 📥
Download videos, photos, and audio from TikTok, YouTube, Instagram, Facebook, and more.
Check it out here: https://niyander.com/projects/tools/sm/
Your feedback is welcome!

Notice

Hey friends! I just launched a free AI Background Remover called Panda AI! 🐼
Easily remove backgrounds from images with just one click.
Try it out now: https://niyander.com/projects/tools/bg/
Let me know what you think!

May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Apr    

About

Greetings, Hey i am Niyander, and I hail from India, we strive to impart knowledge and offer assistance to those in need.

  • Alpine.js Components
  • Android
  • Bootstrap
  • Coursera Hub
  • Genshin Impact News
  • Jobs and Internship
  • Uncategorized

Hot Topics

  • Microsoft Azure Data Scientist Associate (DP-100) Quiz Answers + Review
  • Prepare for DP-100: Data Science on Microsoft Azure Exam Answers + Review
  • Genshin Impact Version 5.6 redeem codes
  • More Software Engineering Jobs in Japan for Expats – April 2025 Edition
© 2025 Niyander Tech | Powered by Minimalist Blog WordPress Theme