jQuery Countdown Plugin - only show non-zero periods

只愿长相守 提交于 2019-12-10 16:05:30

问题


I'm coding a countdown with jQuery countdown plugin.

I only want it to show active ('non-zero') periods, e.g. instead of

time left: 0 Days, 0 Hours, 13 Minutes, 20 Seconds

it should only show

13 Minutes, 20 Seconds.

My code is:

$('#countdown').countdown({
    expiryUrl:'index.php?show=main', 
    expiryText: 'EXPIRED', 
    until: timeleft, 
    layout:'{d<}{dn} {dl} and {d>}{h<}{hn} {hl}, {h>}{m<}{mnn} {ml}, {m>}{s<}{snn}{sl}{s>}'
});

But the problem is that with this code it hides the 'Days' period, but NOT the 'Hours/Minutes'

So currently I get this:

time left: 0 Hours, 10 Minutes, 27 Seconds

What do I have to do to hide ALL zero periods?

Thank you!


回答1:


Hiya please see here adding 2 jsfiddle to show you the difference: http://jsfiddle.net/8JJ9B/6/ (Only the seconds will appear note this happens when you set **format** option of countdown as lower case) and http://jsfiddle.net/8JJ9B/4/ (Zero will appear as well because I have format option set as Capital characters) update http://jsfiddle.net/cUW4M/

To avoid any non-zero value to appear countdown plugin has a reference called format: with options as lower case character

Further http://keith-wood.name/countdownRef.html#format

[Quote]Format option == ...Use upper-case characters for mandatory periods, or the corresponding lower-case characters for optional periods, i.e. only display if non-zero. Once one optional period is shown, all the ones after that are also shown...[Unquote]

code

$('#highlightCountdown').countdown({until: 0, format: 'hmS',onTick: highlightLast5});



回答2:


Simplified version of @Tats_innit's answer (all the correct info is in the comments and jsfiddles): Combining both the format and layout parameters is key.

$('#mytimer').countdown({
    until: timerEnd, 
    compact: true,
    format: 'dhMS',
    layout: '{d<}{dn} days {d>}{h<}{hn}{sep}{h>}{m<}{mnn}{sep}{m>}{s<}{snn}{s>}'
});

This yields, as appropriate:

4 days 22:16:01

11:12:13

06:05

00:01



来源:https://stackoverflow.com/questions/10469714/jquery-countdown-plugin-only-show-non-zero-periods

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