[Solved] Count Down – Java Script


It can’t reach your if statement…

if (weeks < 1 ) will swallow before ‘if (weeks < 1 && days < 1)` …

Switch your if so the test for ‘if (weeks < 1 && days < 1)comes first. Also you are usingdayinstead ofdays. Also, don't declare yourdays` twice – not good practice though it isn’t causing your problem.

More likely you want to get rid of your else in that statement.

Here’s what you want:

   if (weeks < 1) {
        document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' +  seconds + 'secs';
    }

    else if (days < 1) {
        days = Math.floor(distance/ _day);
        document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' +  seconds + 'secs';
    }

    if (weeks < 1 && days < 1) {
        document.getElementById('countdown').innerHTML = hours + 'hrs '+ minutes + 'mins ' +  seconds + 'secs';

You have a number of issues – for example you have day instead of days in your one if branch. Paste this code in place of what you have for the if and you should be a lot closer to what you want.

Here’s what mine looks like with these coding changes:

10hrs 21mins 56secs

Here’s the code to use in place of what you have:

<!DOCTYPE html>
<html>
<body>

<script>
    var end = new Date('Jan 29, 2017 15:37:25');

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

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

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

            return;
        }

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


        if (weeks < 1) {
            document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' +  seconds + 'secs';
        }

        else if (days < 1) {
            days = Math.floor(distance/ _day);
            document.getElementById('countdown').innerHTML = days + 'days ' + hours + 'hrs '+ minutes + 'mins ' +  seconds + 'secs';
        }

        if (weeks < 1 && days < 1) {
            document.getElementById('countdown').innerHTML = hours + 'hrs '+ minutes + 'mins ' +  seconds + 'secs';

        }

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

    timer = setInterval(showRemaining, 1000);

</script>
<p id="countdown"></p>

</body>
</html>

6

solved Count Down – Java Script