Javascript countdown timer
Here’s a nice compact version to display a countdown to a certain date and time on a webpage.
<span id="countdown" class="timer"></span> <script> var upgradeTime = 86401; // seconds remaining var seconds = upgradeTime; function timer() { var days = Math.floor(seconds/24/60/60); var hoursLeft = Math.floor((seconds) - (days*86400)); var hours = Math.floor(hoursLeft/3600); var minutesLeft = Math.floor((hoursLeft) - (hours*3600)); var minutes = Math.floor(minutesLeft/60); var remainingSeconds = seconds % 60; function pad(n) { return (n < 10 ? "0" + n : n); } document.getElementById('countdown').innerHTML = pad(days) + "d:" + pad(hours) + "h:" + pad(minutes) + "m:" + pad(remainingSeconds) + "s"; if (seconds <= 0) { clearInterval(countdownTimer); document.getElementById('countdown').innerHTML = "Completed"; } else { seconds--; } } var countdownTimer = setInterval('timer()', 1000); </script>
Tuesday 7 December 2021, 710 views
Next post: Finding the first and last day of a month in PHP Previous post: Creating a WordPress theme from scratch
Programming index
- Scraping the UK charts – a 2025 guide
- How to post text and images to Bluesky using PHP
- General PHP code snippets
- Date and time utilities in PHP
- How to get rid of “episodes and shows” from Spotify:
- Example of character counter using Bootstrap and jQuery
- How to create a simple Twitter bot
- Finding the first and last day of a month in PHP
- Javascript countdown timer
- Creating a WordPress theme from scratch
Leave a Reply