create a counter in javascript countdown from 30 to 0

Create a JavaScript Countdown Timer from 30 to 0

By Rachel Johnson
AD

Ever needed to create a countdown timer in JavaScript, perhaps for a special offer or an event? Today, we're going to guide you through creating a countdown from 30 to 0 using JavaScript.

Before we dive in, ensure you have a basic understanding of JavaScript and its fundamentals, such as variables and functions. If you're new to JavaScript, don't worry—we'll explain each step in detail.

Setting Up Your HTML Structure

First, you'll need a place to display your countdown. In your HTML file, add a div element with an id of "timer". This will serve as the container for our timer.

AD
Simple Pure Javascript Countdown Timer (Free Download)
Simple Pure Javascript Countdown Timer (Free Download)
JavaScript Countdown Timer With CSS UI | Pure JS Countdown Clock
JavaScript Countdown Timer With CSS UI | Pure JS Countdown Clock
How to Create Countdown animation using CSS
How to Create Countdown animation using CSS

Your HTML structure should look like this:

<body>
<div id="timer"></div>
</body>

Creating the JavaScript Function

Next, we'll create a JavaScript function that will count down from 30 to 0.

Pure CSS Countdown Timer
Pure CSS Countdown Timer
countdown flip clock timer
countdown flip clock timer
the numbers are numbered in red and white for each individual to count on their own
the numbers are numbered in red and white for each individual to count on their own

In your HTML file, add a script tag at the end of the body to contain your JavaScript. Start by defining a function called "countdown" that accepts an argument for the total seconds.

Initializing The Countdown

In the countdown function, convert the total seconds into minutes and seconds for better readability. Then, use a while loop to count down from 30 to 0.

AD

At each iteration, update the timer display and ensure the countdown doesn't go below zero. Here's how you can implement this:

JavaScript Countdown Timer Hours Minutes Seconds
JavaScript Countdown Timer Hours Minutes Seconds
How to create a Countdown component using React & MomentJS
How to create a Countdown component using React & MomentJS
Countdown clock in JS using HTML & CSS | JavaScript Tutorials | Web Development Tutorials
Countdown clock in JS using HTML & CSS | JavaScript Tutorials | Web Development Tutorials
JavaScript Countdown Timer
JavaScript Countdown Timer
25+ Fresh JavaScript Resources | | Graphic Design Junction
25+ Fresh JavaScript Resources | | Graphic Design Junction
Countdown Timer With Pure CSS
Countdown Timer With Pure CSS
function countdown(total_seconds) {
  var minutes = Math.floor(total_seconds / 60);
  var seconds = total_seconds - (minutes * 60);

  if(seconds < 10) {
    seconds = '0' + seconds;
  }
  document.getElementById("timer").innerHTML = minutes + ':' + seconds;

  if(total_seconds > 0) {
    setTimeout(function(){ countdown(total_seconds - 1)}, 1000);
  }
}

Starting the Countdown

Now that we have our function, we need to start the countdown. You can do this by calling the "countdown" function with the total seconds (30 in this case) when the page loads.

Starting on Page Load

Add the following line of JavaScript to make the countdown start as soon as the page loads:

AD

window.onload = function() { countdown(30); }

Bootstrap 5 Counter Up Animation with JavaScript
Bootstrap 5 Counter Up Animation with JavaScript
Countdown Timer using Html CSS & vanilla Javascript | New Year Countdown Clock
Countdown Timer using Html CSS & vanilla Javascript | New Year Countdown Clock
How To Create Countdown Timer Using JavaScript
How To Create Countdown Timer Using JavaScript
Create Time Counter Animation using HTML & CSS!
Create Time Counter Animation using HTML & CSS!
How to Create a Countdown Timer With a Full Screen Background Video
How to Create a Countdown Timer With a Full Screen Background Video
How to Count Number of Executions in JavaScript
How to Count Number of Executions in JavaScript
Count Down Timer Using HTML CSS And JavaScript | Christmas Countdown Timer Using JavaScript
Count Down Timer Using HTML CSS And JavaScript | Christmas Countdown Timer Using JavaScript
the website is under construction, and it's time to be completed with numbers
the website is under construction, and it's time to be completed with numbers
Modern Countdown Timer JS - Code Snippet
Modern Countdown Timer JS - Code Snippet
CSS-Only Countdown Clock
CSS-Only Countdown Clock
The best React countdown timer libraries of 2026
The best React countdown timer libraries of 2026
jQuery Animated Number Counter From Zero To Value - Javascript Animation
jQuery Animated Number Counter From Zero To Value - Javascript Animation
Countdown Timer | JavaScript Project for Beginners
Countdown Timer | JavaScript Project for Beginners
Countdown timer vector clock counter | Free Vector
Countdown timer vector clock counter | Free Vector

Stopping the Countdown

If you want the countdown to stop after it reaches zero, that's already handled in our function. The setTimeout function will stop resetting itself once the countdown reaches zero.

Handling Error or Invalid Inputs

If a user tries to start the countdown with invalid input (like a negative number), the function as is will not gracefully handle this. So, let's add some error handling to our code.

AD

Adding Error Handling

At the beginning of the "countdown" function, add a condition to check if the input is a number and greater than zero. If not, display an error message in the timer div and exit the function.

function countdown(total_seconds) {

  if(typeof total_seconds !== "number" || total_seconds < 1) {
    document.getElementById("timer").innerHTML = "Invalid input, please enter a number greater than zero.";
    return;
  }

  // Rest of the function code...
}

Now, your counter will only start if the input is a number greater than zero. Otherwise, it will display an error message in the timer div.

That's it! You've now created a countdown timer in JavaScript that counts down from 30 to 0. You can now use this function as a template to create other countdowns or timers in your projects.

Ready to take your skills further? Explore more JavaScript tutorials or check out how to incorporate this countdown into a web-based game or application. The possibilities are endless!

AD