imgrooot Posted January 11, 2019 Share Posted January 11, 2019 (edited) Here's the code for the countdown timer. It works. At the end of the countdown, it's suppose to show the message "EXPIRED". But that message only shows once I reload the page. The countdown itself stops at 00:00:01. Is there a way to automatically show the message after that, instead of reloading the page to show it? <style> div#counter{ margin: 100px auto; width: 305px; padding:20px; border:1px solid #000000; } div#counter span{ background-color: #00CAF6; padding:5px; margin:1px; font-size:30px; } </style> <?php $target_date = '2019-01-12 05:40:00'; $timeLeft = (strtotime($target_date) - time()) * 1000; ?> <script src="javascripts/timer.js"></script> <script> $(document).ready(function(){ var timeLeft = <?php echo $timeLeft ; ?>; var timer = new Timer($('#counter'), timeLeft); if (timeLeft <= 0) { $('#counter').text('EXPIRED'); } }); </script> <div id="counter"> <span class="hour">00</span> <span class="min">00</span> <span class="sec">00</span> </div> Edited January 11, 2019 by imgrooot Quote Link to comment Share on other sites More sharing options...
Barand Posted January 11, 2019 Share Posted January 11, 2019 Try using setInterval() to reduce the time left by one second every second Quote Link to comment Share on other sites More sharing options...
requinix Posted January 11, 2019 Share Posted January 11, 2019 Personally I wouldn't use a counter: it's vulnerable to drift since each interval won't fire exactly every 1.000 seconds. Use Date to get the current timestamp and diff it with the start timestamp (which you also got from Date). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.