Countdownjs how to show 0 units

回眸只為那壹抹淺笑 提交于 2019-12-13 04:44:56

问题


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:

Here's what I currently see:

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

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


回答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



来源:https://stackoverflow.com/questions/26769790/countdownjs-how-to-show-0-units

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!