user2886091
user2886091

Reputation: 725

javascript countdown show only hours

I want to countdown to specific date, but I dont want to display years, months, days, minutes and seconds, but only hours. I have following code:

    <script type="text/javascript">
var end = new Date('02/5/2014 10:1 AM');

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;

function showRemaining() {
    var now = new Date();
    var distance = end - now;
    if (distance < 0) {

        clearInterval(timer);
        document.getElementById('countdown').innerHTML = 'EXPIRED!';

        return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor((distance % _day) / _hour);
   // var minutes = Math.floor((distance % _hour) / _minute);
   // var seconds = Math.floor((distance % _minute) / _second);

    document.getElementById('countdown').innerHTML = days + 'days ';
    document.getElementById('countdown').innerHTML += hours + 'hrs ';
   // document.getElementById('countdown').innerHTML += minutes + 'mins ';
   // document.getElementById('countdown').innerHTML += seconds + 'secs';
}

timer = setInterval(showRemaining, 1000);
</script>

When I comment out minutes and seconds It will display days and hours. When I comment out days it will display something like this: 14hrs14hrs14hrs14hrs14hrs14hrs14hrs and so on.

How can I display only hours?

thank you

Upvotes: 0

Views: 1130

Answers (1)

DontVoteMeDown
DontVoteMeDown

Reputation: 21465

It happens because you are concatenating it over gain each time. Just remove the + operator:

document.getElementById('countdown').innerHTML += hours + 'hrs ';

to

document.getElementById('countdown').innerHTML = hours + 'hrs ';

A little time more on this and you could achieve it by yourself.

Upvotes: 1

Related Questions