1

I need help with a countdown. I used the following script to generate a countdown.

var endTime = new Date(2016, 04, 10).getTime() / 1000;

function setClock() {
    var elapsed = new Date().getTime() / 1000;
    var totalSec =  endTime - elapsed;
    var d = parseInt( totalSec / 86400 );
    var h = parseInt( totalSec / 3600 ) % 24;
    var m = parseInt( totalSec / 60 ) % 60;
    var s = parseInt(totalSec % 60, 10);
    var result = d;
    document.getElementById('timeRemaining').innerHTML = result;
    setTimeout(setClock, 1000);
}

setClock();

I just want to see the hours - in this moment, when I delete the days, minutes, seconds, it will just show 24 hours even if it's a longer time.

Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Possible duplicate of [Code for a simple JavaScript countdown timer?](http://stackoverflow.com/questions/1191865/code-for-a-simple-javascript-countdown-timer) –  Apr 06 '16 at 22:56

2 Answers2

0

If i undertand your question, this should work :

function setClock() {
    var elapsed = new Date().getTime() / 1000;
    var totalSec =  endTime - elapsed;
    document.getElementById('timeRemaining').innerHTML =  parseInt( totalSec / 3600 );
    setTimeout(setClock, 1000);
}
osmanraifgunes
  • 1,438
  • 2
  • 17
  • 43
0

If you only want to count down the hours until your date (may 10, 2016), here's one solution for you:

var endTime = new Date('2016/05/10').getTime()/1000;

function setClock(){
  var currDate = new Date().getTime()/1000;

  var timeRemaining = Math.ceil((endTime - currDate)/(60*60));
  /*time remaining is in hours. Math.ceil will make sure that 1 hour is shown
    if time remaining is less than 1 hour unless you want to show mins */

  document.getElementById('timeRemaining').innerHTML = timeRemaining; 

  setTimeout(setClock, 1000);
}

setClock();

https://jsbin.com/buxetenika/edit?html,js,output

This should get you started...hope this helps

zod799
  • 51
  • 3