
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to create a countdown timer with JavaScript?
To create a countdown timer with JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .timer { text-align: center; font-size: 60px; margin-top: 0px; color: white; background-color: rgb(100, 38, 214); } </style> </head> <body> <h1 style="text-align: center;">Countdown Timer Example</h1> <h2 class="timer"></h2> <script> var countDownDate = new Date("June 5, 2022 11:27:15").getTime(); var timeClear = setInterval(function() { var now = new Date().getTime(); var timeLeft = countDownDate - now; var days = Math.floor(timeLeft / (1000 * 60 * 60 * 24)); var hours = Math.floor( (timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60) ); var minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((timeLeft % (1000 * 60)) / 1000); document.querySelector(".timer").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; if (timeLeft < 0) { clearInterval(timeClear); document.querySelector(".timer").innerHTML = "Timer Finished"; } }, 1000); </script> </body> </html>
Output
The above code will produce the following output −
- Related Articles
- How to set a countdown timer in javascript?
- How to make a countdown timer in Android?
- Making a countdown timer with Python and Tkinter
- How to create a timer using tkinter?
- How to create a high resolution timer with C++ and Linux?
- How to create timer using C++11?
- Python Program to Create a Lap Timer
- How to create a line break with JavaScript?
- How to create a filter table with JavaScript?
- How to create a typing effect with JavaScript?
- How to create a filter list with JavaScript?
- How to create a table caption with JavaScript DOM?
- How to create a session only cookies with JavaScript?
- How to create an Autocomplete with JavaScript?
- How to create a responsive slideshow with CSS and JavaScript?

Advertisements