Brian Nguyen
Brian Nguyen

Reputation: 102

Why is my countdown function not working?

I was so excited for Christmas that I decide what better way to countdown than create a countdown timer to Christmas. But my countdown does not appear to work.


let targetDate = new Date(null, 11, 31, 0, 0, 0, 0)

let today = new Date()

targetDate.setFullYear(today.getFullYear())

if (targetDate.getTime() - today.getTime() <= 0) {

    targetDate.setFullYear(today.getFullYear() + 1)

}

let timer = setInterval(() => {

    today = new Date()

    e = document.getElementById("countdown")

    if (targetDate.getTime() - today.getTime() == 0) {

        e.innerHTML = "Horray! Merry Christmas, Everyone! Horray!"

        clearInterval(timer)

    } else {

        distance = targetDate.getTime() - today.getTime()

        days = Math.floor(distance / (24 * 60 * 60 * 1000))

        distance -= day * 24 * 60 * 60 * 1000

        hours = Math.floor(distance / (60 * 60 * 1000))

        distance -= hours * 60 * 60 * 1000

        minutes = Math.floor(distance / (60 * 1000))

        distance -= minutes * 60 * 1000

        seconds = Math.floor(distance / 1000)

        e.innerHTML = days + " d " hours + " h " + minutes + " m " + seconds + " s "

    }

}, 1000)

I have tried console logging the timer value but the code didn't rendered. It appears that the innerHTML code didn't run.

Upvotes: 2

Views: 75

Answers (1)

Shevtsov
Shevtsov

Reputation: 140

Look at this line

 e.innerHTML = days + " d " hours + " h " + mi

it should be

 e.innerHTML = days + " d " + hours + " h " + mi

Upvotes: 2

Related Questions