4

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!

Iman
  • 459
  • 4
  • 19
user1377886
  • 43
  • 1
  • 4

2 Answers2

3

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});
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • Thanks for your answer Tats_innit, yes I could do this with "format", but I want to use "layout". As I wrote, in my example "zero Days" disappears, but not zero Hours/Minutes. Maybe it's a bug? The documentation http://keith-wood.name/countdownRef.html#layout says: "If you need to exclude entire sections when the period value is zero and you have specified the period as optional, surround these sections with '{p<}' and '{p>}', where p is the same as above." Hm, what is meant by "and you have specified the period as optional" ? How can I specify Hours/Minutes as optional? – user1377886 May 06 '12 at 11:40
  • @user1377886 hiya cooleos! can you try using one of the jsfiddle and flick it across I might take a look bruv :) or might be something else; have a nice one, cheers! – Tats_innit May 06 '12 at 11:45
  • OK, have a look here: http://jsfiddle.net/8JJ9B/9/ It counts back 100 seconds (1m 40s), it doesn't show DAYS but it shows HOURS, even if they are zero – user1377886 May 06 '12 at 11:50
  • Hiya @user1377886 got you bruv- and it seems still evolving check this link: http://keith-wood.name/countdown.html and if you go to **second unorderlist demo** and click show code - you will notice it uses **format** hence to achieve your desired output I made a minor change to the fiddle **here:** jsfiddle: http://jsfiddle.net/HjQSF/22/ and Hope this helps! have a nice one, :) cheerios! – Tats_innit May 06 '12 at 12:26
  • Thank you this works perfectly! So one must combine both format and layout! – user1377886 May 06 '12 at 12:47
0

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

zero323
  • 322,348
  • 103
  • 959
  • 935
squarecandy
  • 4,894
  • 3
  • 34
  • 45