Countdown Timer

Hi, I’m trying to create a timer that counts down from 1 hour in minutes and seconds. I want the timer to show as being in a box and to start when a button is pressed. Any ideas?

1 Like

This code can easily be modified and started on button click instead of page onReady.

$w.onReady(function () {
 var countDownDate = new Date("Dec 1, 2018 12:59:25").getTime(); // Countdown date and time

 var x = setInterval(function() {
 var now = new Date().getTime();
 var distance = countDownDate - now;
 var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
 var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
 var seconds = Math.floor((distance % (1000 * 60)) / 1000);
 
        $w("#countDownTimer").text = hours + "h " + minutes + "m " + seconds + "s ";
 
 if (distance < 0) {
            clearInterval(x);
            $w("#countDownTimer").text = "EXPIRED";
        }
    }, 1000);
 
});


Also remember that this way might make the browser stop responding because it runs code in the local browser.

@andreas-kviby I’m not trying to countdown to a date. Just a simple one hour timer shown in minutes and seconds that starts on a button click

1 Like

used the code as the base for my countdown it is great,thank you