Skip to content Skip to sidebar Skip to footer

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. Her

Solution 1:

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


Post a Comment for "Countdownjs How To Show 0 Units"