Jump to content
imgrooot

Need a bit of help with the countdown timer.

Recommended Posts

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 by imgrooot

Share this post


Link to post
Share on other sites

Try using setInterval() to reduce the time left by one second every second

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.