To implement a countdown timer using jQuery, you can follow these steps:
html<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
html<div id="countdown"></div>
html<script>
$(document).ready(function() {
// Set the date and time for the countdown (replace with your desired date and time)
var countdownDate = new Date("Feb 28, 2024 00:00:00").getTime();
// Update the countdown every 1 second
var countdownInterval = setInterval(function() {
// Get the current date and time
var currentDate = new Date().getTime();
// Calculate the remaining time in milliseconds
var timeRemaining = countdownDate - currentDate;
// Calculate days, hours, minutes, and seconds
var days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
var hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
// Display the countdown in the specified element
$("#countdown").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");
// Check if the countdown has reached zero
if (timeRemaining < 0) {
clearInterval(countdownInterval);
$("#countdown").html("EXPIRED");
}
}, 1000); // Update every 1 second
});
</script>
Replace the countdownDate
variable with the date and time you want the countdown to end. This example displays the countdown in days, hours, minutes, and seconds. If the countdown reaches zero, it will display "EXPIRED."
Adjust the styling and format as needed for your specific project.