Skip to content
nyedidikeke edited this page Dec 12, 2016 · 1 revision

Welcome to the simple-js-countdown-timer wiki!

Include countdowntimer.js in your page.

Run countdown at button click

HTML

<button>Start Count Down</button>
<div><span id="timer"> (HH:mm:ss)</span>

Javascript

window.onload = function () {
    var display = document.querySelector('#timer'),
        timer = new CountDownTimer(3600),
        timeObj = CountDownTimer.parse(3600);

    format(timeObj.hours, timeObj.minutes, timeObj.seconds);

    timer.onTick(format);

    document.querySelector('button').addEventListener('click', function () {
        timer.start();
    });

    function format(hours, minutes, seconds) {
        hours = hours > 0 ? "0" + hours : "0" + hours;
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = hours + ':' + minutes + ':' + seconds;
    }
};

Clone this wiki locally