Countdown timer daily loop

I’m want to use a countdown timer to count to 10am every day so I am using this:

setInterval(function time(){
  var d = new Date();
  var hours = 09 - 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('#countdown p').html('<span>'+hours+'</span><span class="mins">'+min+'<br></span><span class="secs">'+sec+'</span>')
}, 1000)

However, after 10am it obviously wants to turn negative, so I want to add in something to add 24hr after 10am like:

  if(hours >= 10){
      d = new Date() + 1;
  }

but cannot get it working, any thoughts would be greatly appreciated.

Answer

You want to set hours and then use getDate() method.

setInterval(function time(){
  var start = new Date;
  start.setHours(10, 0, 0); // 10am
  var now = new Date;
  if (now > start) { // check current time is getter then add one day
      start.setDate(start.getDate() + 1);
  }


var days = ((start - now) / 1000);
    var hours = format((days / 60 / 60) % 60);
    var min = format((days / 60) % 60);
    var sec = format(days % 60);

jQuery('#countdown p').html('<span>'+hours+'</span><span class="mins">'+min+'<br></span><span class="secs">'+sec+'</span>')
},1000);

// Add before 0 of hour, min, sec
function format(num) {
    return ("0" + parseInt(num)).substr(-2);
}