0
votes

so I'm trying to create a timer to count down every 12 hrs based on my web host Time Zone (GMT+10), for example, amount of time till 12am than once 12am passes how long till 12pm and so on. I currently have some code but I believe it only counts down 24 hrs...

setInterval(function time(){
  var d = new Date();
  var hours = 24 - d.getHours();
  var min = 60 - d.getMinutes();
  if((min + '').length == 1){
    min = '0' + min;
  }
  var sec = 60 - d.getSeconds();
  if((sec + '').length == 1){
        sec = '0' + sec;
  }
  jQuery('#the-final-countdown p').html(hours+':'+min+':'+sec)
}, 1000);

Oh and a quick little bonus question... If possible would any one know (if their is any way to make this timer) when the timer reaches 00:00:00 to display text for 30 seconds than resume the count down?

1
Java != javascript... - assylias

1 Answers

0
votes

This should do the trick, %12 will make it reach 0 two times a day.

Should be 23 and 59, not 24 and 60 because counting start from 0

setInterval(function time(){
  var d = new Date();
  var hours = (23 - d.getHours())%12;
  var min = 59 - d.getMinutes();
  var sec = 59 - d.getSeconds();


    if(min===59&&sec>30&&hours===11){
  min=0;
  sec=0;
  hours=0;
  //and show what you want for 30 seconds
  

  }
   if((min + '').length === 1){
    min = '0' + min;
  }
  
  if((sec + '').length === 1){
        sec = '0' + sec;
  }
  jQuery('p').html(hours+':'+min+':'+sec)
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>