Search code examples
javascriptjquerycountdowncountdownjs.js

Countdownjs how to show 0 units


I am using the JS library CountdownJS. I am wanting to return a zero value for each of the time units when applicable. Currently when the value is 0 the unit just doesn't show.

Here's what I want to see:

enter image description here

Here's what I currently see:

enter image description here

Anyone familiar with the library know where in the source I would look to update this?

Ref: http://countdownjs.org/readme.html


Solution

  • Here's a really simple countdown function I wrote a while back. Feel free to use it however.

    var t = new Date(2014,11,25,9,30),
        p = document.getElementById("time"),
        timer;
    var u = function () {
        var delta = t - new Date(),
            d = delta / (24 * 3600 * 1000) | 0,
            h = (delta %= 24 * 3600 * 1000) / (3600 * 1000) | 0,
            m = (delta %= 3600 * 1000) / (60 * 1000) | 0,
            s = (delta %= 60 * 1000) / 1000 | 0;
        
        if (delta < 0) {
            clearInterval(timer);
            p.innerHTML = "timer's finished!";
        } else {
            p.innerHTML = d + "d " + h + "h " + m + "m " + s + "s";
        }
    }
    timer = setInterval(u, 1000);
    <h1 id="time"></h1>

    The only tricky part might be my use of

    h = (delta %= 24 * 3600 * 1000) / (3600 * 1000) | 0
    

    delta %= ... returns delta, after performing the %=. This was just to save characters. If you don't like this, you can just separate the delta %= ... part:

    delta %= 24 * 3600 * 1000;
    h = delta / (3600 * 1000) | 0;
    // ... do the same for the rest
    

    fiddle