简体   繁体   中英

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:

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM