Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. JavaScript
  4. Countown Timer in Javascript

Countown Timer in Javascript

Date- Sep 17,2022 Updated Jan 2026 5011
countdown timer Timer in js

What is a Countdown Timer?

A countdown timer is a timekeeping device that counts down from a specified time to zero. It is widely used in applications where time management is crucial, such as countdowns for events, deadlines, and even in games. The timer visually represents the time remaining, providing users with a clear indication of how much time they have left.

In web development, countdown timers enhance user experience by adding a sense of urgency or excitement. They can be used for promotions, sales, or any event that has a defined time limit. For instance, an e-commerce website might use a countdown timer to create urgency for a limited-time offer.

Prerequisites

Before diving into the code, ensure you have a basic understanding of HTML, CSS, and JavaScript. Familiarity with DOM manipulation in JavaScript will be beneficial as we will be interacting with HTML elements to display the countdown timer.

Creating the HTML Structure

To create our countdown timer, we will first need a simple HTML structure. This includes a <div> or <span> element where the timer will be displayed. Below is an example of how you can set up your HTML:

<div class="jumbotron"> <h1>JavaScript Countdown Timer</h1> </div> <div id="timer" style="font-size:30px"> 5:00 </div>

In this example, we have a heading that describes the timer and a div with an ID of timer that will display the countdown starting at 5 minutes.

Countown Timer in Javascript

Implementing the Countdown Timer Logic

Next, we will write the JavaScript code to implement the countdown timer functionality. This involves setting up a function that updates the timer every second. Here is the script that you can add to your page:

setTimeout(startTimer, 1000);

var ResetTimer = function () {
    document.getElementById('timer').innerHTML = "5:00";
};

function startTimer() {
    var presentTime = document.getElementById('timer').innerHTML;
    var timeArray = presentTime.split(/[:]+/);
    var m = timeArray[0];
    var s = checkSecond((timeArray[1] - 1));
    if (s == 59) { m = m - 1; }
    if (m < 0) { return; }
    document.getElementById('timer').innerHTML = m + ":" + s;
    if (parseInt(m) == 0 && parseInt(s) == 0) { ResetTimer(); }
    console.log(m);
    setTimeout(startTimer, 1000);
}

function checkSecond(sec) {
    if (sec < 10 && sec >= 0) { sec = "0" + sec; }
    if (sec < 0) { sec = "59"; }
    return sec;
}

In this code, we start the timer with a default value of 5 minutes. The startTimer function updates the time every second. If the seconds reach zero, the timer resets back to 5 minutes.

Enhancing the Timer with User Input

To make our countdown timer more interactive, we can allow users to set their own time. This can be achieved by adding an input field where users can specify the desired countdown time. Below is an example of how to implement this:

<input type="text" id="customTime" placeholder="Enter time in mm:ss">
<button onclick="setCustomTime()">Start Timer</button>

Next, we will need to add a function to handle the custom time input:

function setCustomTime() {
    var userInput = document.getElementById('customTime').value;
    document.getElementById('timer').innerHTML = userInput;
    startTimer();
}

With this implementation, users can enter their desired countdown time in the format of minutes and seconds, and the timer will start from that value.

Styling the Countdown Timer

To make our countdown timer visually appealing, we can apply some CSS styles. Below is an example of how you can style the timer:

#timer {
    font-size: 50px;
    color: #FF5733;
    font-weight: bold;
    text-align: center;
}

Feel free to customize the styles to match your website's theme. You can also add animations or transitions to make the countdown more engaging.

Countown Timer in Javascript 2

Edge Cases & Gotchas

When implementing a countdown timer, there are a few edge cases to consider:

  • Negative Time: Ensure that the timer does not allow negative values. This can happen if the user manually enters an invalid time.
  • Multiple Timer Instances: If a user starts multiple timers, ensure that they do not interfere with each other. You may want to disable the start button once a timer is running.
  • Browser Compatibility: Test your countdown timer across different browsers to ensure consistent behavior.

Performance & Best Practices

To ensure optimal performance of your countdown timer, consider the following best practices:

  • Efficient DOM Manipulation: Minimize DOM updates to improve performance. For example, avoid updating the timer display if the time hasn't changed.
  • Use requestAnimationFrame: Instead of using setTimeout, consider using requestAnimationFrame for smoother animations and better performance.
  • Accessibility: Ensure that your timer is accessible to all users, including those using screen readers. Provide appropriate ARIA labels and roles.

Conclusion

In this tutorial, we covered how to create a countdown timer in JavaScript from scratch. We explored how to implement the timer logic, allow user input, and style the timer for better presentation. Here are the key takeaways:

  • Countdown timers are useful for various applications, enhancing user experience.
  • JavaScript can be used to create interactive countdown timers with user input.
  • Consider edge cases and performance best practices while implementing your timer.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Complete Guide to CSS Assignment 1: Step-by-Step Explained
Dec 09, 2023
Mastering Chapter 1 CSS: A Complete Guide with Examples in HTML/CSS
Dec 09, 2023
Attribute of table in HTML
Dec 09, 2023
Anchor and Image tags in HTML
Dec 09, 2023
Previous in JavaScript
Audio and Video Call Integration using QuickBlox
Next in JavaScript
Input Mask in Jquery
Buy me a pizza

Comments

On this page

๐ŸŽฏ

Interview Prep

Ace your JavaScript interview with curated Q&As for all levels.

View JavaScript Interview Q&As

More in JavaScript

  • Complete Guide to Slick Slider in JavaScript with Examples 14883 views
  • Card Number Formatting using jquery 11591 views
  • Alphanumeric validation in JavaScript 8803 views
  • Jquery Autocomplete 8419 views
  • Input Mask in Jquery 7481 views
View all JavaScript posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor