Things you might find useful

Apple-Style Counter

February 12th, 2010

When I saw the 10 billion song counter on Apple’s website yesterday, I immediately right-clicked on it to see if it was flash. No big surprise to see that it wasn’t, this being Apple, and when I saw it was done using JavaScript I knew how I’d be wasting the next few hours.

Counter from apple.com

Apple’s 10 billion song countdown.

Update: November 15, 2010: New version available

My first instinct was to use Chrome’s developer tools to inspect the page and check out the JavaScript for the counter to get some clues as to how to go about re-creating it. After a few seconds of looking at Apple’s minified code, I knew I’d get no help there. After grabbing the digits and comma graphic they used, I set out to avoid the work I was supposed to be doing to create a counter from scratch using these two images and jQuery.

Here it is

I’ll give a brief overview of the steps taken to reproduce the counter, but for those that want to cut to the chase, here it is. The code is commented enough so that anyone interested can view the source and see what’s going on.

View my Apple-style counter

Note: This counter did not actually track the number of iTunes downloads, but is an example of how to create a similar counter for your own purposes.

Be sure to read all the way down for more demos.

Basics

Looking at the image closely, we can see that each frame is 103px in height, with five frames of animation between digits, leaving 618px of space between each digit. So, digit zero would have a background position of '0 0', one would be '0 -608px', two would be '0 -1236px', etc. My plan was to use an unordered list to hold the digits and use jQuery to animate the background position of each list item.

<ul>
    <li id="d9"></li>
    <li class="seperator"></li>
    <li id="d8"></li>
    <li id="d7"></li>
    <li id="d6"></li>
    <li class="seperator"></li>
    <li id="d5"></li>
    <li id="d4"></li>
    <li id="d3"></li>
    <li class="seperator"></li>
    <li id="d2"></li>
    <li id="d1"></li>
    <li id="d0"></li>
</ul>

And the CSS:

*{
    margin:0;
    padding:0;
}
ul{
    list-style-type:none;
    width:566px;
    margin:50px auto;
}
li{
    float:left;
    background:url(filmstrip.png) 0 0 no-repeat;
    width:53px;
    height:103px;
}
li.seperator{
    background:url(comma.png) 2px 75px no-repeat;
    width:12px;
}

That’s it for HTML and CSS, the rest is JavaScript.

Making it work

It took no time to get a simple counter running, but the hairy part was triggering a five-frame animation for each digit that needed to change. Because I wanted to mimic the functionality of the counter on Apple’s site, it needed to look like it was counting at a very fast increment, with variable animation speeds depending on the digit.

After incrementing the value, the first step is to see which digits need to be animated:

function digitCheck(x,y){
    var digitsOld = splitToArray(x),
    digitsNew = splitToArray(y);
    for (var i = 0, c = digitsNew.length; i < c; i++){
        if (digitsNew[i] != digitsOld[i]){
            animateDigit(i, digitsOld[i], digitsNew[i]);
        }
    }
}

This takes inputs x (pre-increment value) and y (post-increment value), and sends them to a helper function I wrote called splitToArray, which breaks each value into an array of individual digits. It then compares each digit, and if there is a change, passes the info on to animateDigit.

Inside animateDigit, the most difficult part was getting the animations for each digit cued up correctly. Inside the loop that controls the animation, I needed to slide the background position down five times with a specific delay, and my first solution was using setTimeout, and the statement:

setTimeout(doAnimate, timeOut, n, pos);

This worked great in Chrome and FF, and I thought I was done... until I checked it in IE. I had forgotten you can't pass parameters to functions with setTimeout in this way, so you need to use a closure:

setTimeout(function(){doAnimate(n,pos)}, timeOut);

Simple enough, I thought, but that made the animation choke up in every browser. No good.

I slept on it, then this morning thought about using jQuery's new delay method added in version 1.4. Boom! I was able to do away with the doAnimate function all together, and kill it all with one pass:

$("#d" + n).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0);

I now had cross-browser harmony, and had once again spent time away from paying projects to build something I have absolutely no use for :)

Customizing

When making this demo, I chose to use the same Apple graphics so when it was finished, I could look at the two side-by-side and see if I got the functionality correct. This post is just me sharing my learning process with you, so if in the future you ever have the need for a count down/up graphic, maybe this will help. The impression shouldn't be "how to rip-off Apple's look", but rather "how to have the same functionality in my own projects."

Customizing this to your own graphics is very easy. All you need is a consistent spacing between your animation frames, and be able to add. You can see a hastily-made custom example here. In it, my frames are 50 x 80px and I too have 5 animation frames between digits. Most of the customization happens at the beginning of the JavaScript:

// Array to hold each digit's starting background-position Y value
var initialPos = [0, -480, -960, -1440, -1920, -2400, -2880, -3360, -3840, -4320];
// Amination frames
var animationFrames = 5;
// Frame shift
var frameShift = 80;

The initialPos array holds the starting Y value of each digit, in increments of 480 (80px per frame x 6 frames per digit). animationFrames says there are 5 animation frames between digits, and frameShift is the frame height. Those are the only changes needed to make it function properly (besides updating the CSS), and further animation tweaking can be done in the function animateDigit.

Update 1

Here is another example demonstrating how to make a clock using the same animation technique.

Update 2

My original demo was based on counting to 10 billion, so had a strict number of digits. I realize that is not the most practical application, so I made a dynamic counter example where the JavaScript takes care of HTML for each digit, as well as commas when necessary. You can set whatever number you want via the URL query string (set n = number), or view the page by itself to just generate a random number. View the source to see different ways of setting a start number for the counter to use.

Update 3

Example with zero-filled numbers.

Update 4

As per many requests, and the help of a visitor who made three new digit graphics, I have yet another example of this animation technique in action. This time it is not counting up, but rather down—to any date in the future, in 'days : hours : minutes : seconds' format. It handles all HTML and formatting itself, all you need to do is plug in a date.

View countdown demo

Warning: Don't use the countdown script in a live site, because the jQuery animation technique used will not work properly in Chrome, see bug here.

It's set to countdown to Christmas of this year, but you can quickly test different dates by adding a date to the URL in this format: '?date=M/D/YY', e.g. '?date=6/1/11'. It uses the @font-face font kit BD Cartoon Shout from Font Squirrel. As usual, code is commented if you're curious. Here is everything for the countdown in a zip file.

Update: November 15, 2010

I've rewritten entire script and added lots of new functionality:

View revisited counter

Update: February 7, 2011

This script will not work properly with jQuery 1.5, and will no longer be maintained. Think of this post as a first draft of me trying to get the animation working; the code is outdated. If you want to use this type of counter please use the newer version of the script, which will be maintained:

View revisited counter

Warning: Don't use the countdown script in a live site, because the jQuery animation technique used will not work properly in Chrome, see bug here.

If you have any questions, comments or suggestions; please comment or email me.

Tags: , ,

  • ben goshow

    holy crap. i can’t believe we had you debugging dreamweaver HTML at Lumasis. ;)

  • bit4fox

    Nice Counter :)
    But it doesn’t work correctly. Look at your counter, and you’ll see, that the digits change before the have to.
    Example: The counter shows 433. Then it changes to 534 instead of waiting to 499 and then changing to 500.

    bit4fox

  • http://none Sulaiman

    Just WOW!
    impressive work!
    Thank you very much!
    You made my day!
    Thank you for sharing this!

  • http://cnanney.com Chris Nanney

    bit4fox,

    You are correct, this is by design. Because I was replicating the Apple counter, which is counting very fast, my counter is incrementing by 17 every 180 milliseconds.

    If that was changed to increment by 1 at a slower interval, say 500 milliseconds, you would see it count properly – one digit at a time.

  • bit4fox

    Thank you very much, Chris Nanney :)

    Now I’m happy ;)

    bit4fox

  • shortyjr

    Hi. The pictures that contains every number from where i will download them?

  • http://cnanney.com Chris Nanney

    If you view source on the demo and look in the CSS, you’ll see the background image used for the digits.

  • http://www.dirxion.com David Dames

    This is a great tutorial. I’ve been trying to do something similar, but I’m too new to JavaScript and Jquery. So, I have a question. How would I keep the starting variable updated so it doesn’t keep starting over? I would like to have this on the server, but don’t know how to plug that in.

  • http://cnanney.com Chris Nanney

    You can easily set the starting number from a GET request, I just emailed you an example.

  • Brandon

    This is awesome! Been looking for this code since I saw it on apple.com. Any way to have it show 00,000,000,324 with the leading zeros? My counter will need to count up to 72 billion.
    Thx!

  • http://cnanney.com Chris Nanney

    Brandon,

    I’ve updated the post with an example of zero-filled numbers. Two changes to doCount() and initialDigitCheck(), and a simple padding function are all you need.

  • Brandon

    Excellent! Thanks man!

  • Tom

    I feel like I’m a pain by asking this. By any chance do you have a clock-like countdown interpretation of this method? Such as counting down to a certain date/time.

  • http://cnanney.com Chris Nanney

    Switching to a countdown would require making a new filmstrip graphic. For example, currently as digit 1 falls, it reveals 2 – that would need to be 0. It might be something I play around with when I’m bored, but have no plans to implement in the near future.

  • Brandon

    I am having a problem with the script freezing up my browser (Firefox) if left open for a long period of time. Just curious if you have a solution for this. Seems like a memory leak, but I’m no expert. Other than that it is working like a charm.
    Thx!

  • http://cnanney.com Chris Nanney

    I’ve tested it for long periods in Chrome, and memory usage stayed consistent. I haven’t looked in to it with Firefox – I’ll do so.

  • jerm

    noiiiice moves man!
    i would also like to see a countdown timer as well. i will try to work on the image of making a new filmstrip but cant guarantee it’ll be anything remotely close to yours.
    thanks though!

  • http://cnanney.com Chris Nanney

    someone already sent me a new graphic to use for the countdown, and i’ll work on getting a days, minutes, seconds countdown demo up in the next few days.

  • kong

    Fantastic work.

    However the animation is not as nice as the one on apple website. You got numbers right, you got fading right, but the flipping between digits is not smooth.

  • Dejan

    Hi Chris,

    Great job on reconstructing this effect. Have you had any success with the countdown demo? I think the same graphic can be reused. It doesn’t really have to drop “down”, if the transition is done fast enough.

    I’d like to help out. Let me know if you’d like to share the graphic you have. I might put it up together if you don’t have the time or interest.

    Cheers!

  • http://cnanney.com Chris Nanney

    Thanks Dejan,

    I’ve got the countdown timer just about ready to go, just a little more cleaning to do. I’ll be updating the post with it in a day or two.

  • Dejan

    Thanks Chris, that looks awesome!

  • bit4fox

    I can tell you why it is: The counter on Apple’s page counts very fast, but the counter here doesn’t do ;)

  • http://cnanney.com Chris Nanney

    Correct. In the code you’ll see where I’ve set “var pace = 180;”. Make that a lower number to speed up the counting and the animation. I made it a little slower to make the slowest digit look more realistic in terms of how an old flip clock would look with real gravity.

    Slowing it down a little and still only having 5 frames of animation does make it look a little choppier, but if you change 180 to something like 120, you’ll see a big difference, and it will look as smooth as Apple’s did. They both are using the same graphic and animation technique.

    I tried to comment the code enough so that people could tweak it to their liking, and it is meant to show you the technique of animation, not compare visually 100% to Apple’s.

  • sid

    hey chris,

    I wanted to display this counter at 60% of the original size, I changed the image to be 60% of the original size. I updated all the CSS as well.

    I think I’m doing something wrong wiht the starting Y-positions…what exactly shoudl I be doing here? I tried changing each number to be 70% of the number you had listed…but doesnt seem to do the trick…

  • cnanney

    Without a sample to look at, I imagine that the frames aren't lining up correctly. For this animation to work, all animation frames need to be exact to the pixel. If you just scaled the filmstrip image down 60% in Photoshop for example, there will be some rounding going on there, and there will be 1-2 pixel variances between frames that will make the animation jumpy.

    I would suggest going over the resized image frame by frame to ensure that each is the same height, and then you will be able to calculate new Y values for the javascript (you'll also need to change the variable 'frameShift' to the new frame height value).

  • Sid

    Can you tell me a little more about how this works:
    var initialPos = [0, -433, -866, -1299, -1732, -2165, -2598, -3031, -3464, -3897];

    which value corresponds to d0, d1, d2…? I tried changing different value in he intialpos, but am having a tougth time tracking down what is changing exactly

    here is my counter:

  • Sin

    this is an excellent script you have here. i have one question, how could you use this to set a specific number, let's say, a pre set amount in php. like if i have 205 posts, the counter will show that amount. can this be done? Thanks ahead of time, and keep up the cool work :P

  • http://cnanney.com Chris Nanney

    The example given for update #2, here, demonstrates how to set custom numbers for the counter. Look at the comments in the code for more info.

  • Daniele

    Hey Chris, great work! You saved my life, this count down timer works perfect on safari and firefox, but it does not even show up in internet explorer. Is there anything I'm missing out?

  • http://cnanney.com Chris Nanney

    All the demos were tested and work fine for me in IE6 & 7. I don't have IE8 installed, but don't see how it wouldn't work there. I'm not sure why you are having difficulties with IE.

  • Daniele

    Hi Chris, thank you for your super fast reply! I have tested the counter on IE6 and IE7 using a browser simulator (IE tester) and tried directly with IE8 on windows xp. I've noticed you talk about compatibility quoting this: “$(“#d” + n).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0);”. In the code I'm using I see this light difference: $(“#” + which).delay(speed).animate({'background-position': '0 ' + pos + 'px'}, 0); Is everything ok with this?

    Thank you for your help!

  • http://cnanney.com Chris Nanney

    All the code I talk about in the post pertains to the initial counter demo. As I wrote more demos, the code would change a little, e.g. on the countdown demo I no longer use an array for the digits, I just keep them as string and use .charAt to access them. The difference you pointed out is more of the same. In the original code the digit IDs were prefixed with a 'd', so thats why you see $(“#d”+n) as the selector, and in the counter demo it's $(“#”+which), but they are doing the same thing.

    The original problem with compatibility was when I was using setTimeout to control the animation, but when I switched to jQuery's delay method there were no more issues.

    So on your winXP machine, with IE8, none of the demos work?

  • http://twitter.com/terminal8 terminal8

    Hi Chris, is there any change to get the source images for countdown? I would love to use it, but the current graphic is unusable on dark background because PNG alpha channel is not used and shadows are just bitmaps.

  • http://cnanney.com Chris Nanney

    My original source image was the one Apple used on their counter, which did not use alpha channels. A reader created the countdown graphic for me I believe by just manipulating the original graphic. I don't have a master copy that has true transparency.

  • Brian

    Hey Chris, I skimmed the comments below, but how hard would it be to get it to increment from the thousandths digit up? With a . then the , ? Figured it was worth asking.

  • http://cnanney.com Chris Nanney

    Do you mean incrementing by values over 1,000; i.e. 2,467 -> 3,689 -> 4,899? What do you mean by “.”? None of the examples have decimals – are you wanting 1,234.56 and increment that from the thousandths digit up? I'm not sure I understand what you're asking, can you be a little more specific?

  • Me

    Got it squared… Fun code though. Now I'm just trying to get three of these turkeys goin' at once on a page.

  • androidmanifest

    Hi Chris,

    Awesome coding.
    Can you help with how to make the counter save its state if the page gets refreshed (i am sure many are interested in this) and also increment at a random pace?

  • http://cnanney.com Chris Nanney

    There are a few ways you could go about saving counter state.

    You could save the current number to a cookie on window.onunload, and then where declaring variables check to see if the cookie exists and use that value if it does. But then if they visit the site two hours later, it would start back up where they left off, which might not be a good thing.

    A better way is figuring out where the counter should start on the server side. Say I'm using the counter to display number of downloads, and I know that I'm averaging 3 downloads per second. When the page loads, I'd dynamically set the start number by pulling the actual number from the database, and the JavaScript would increment that number by 3 every second. Of course it's not completely accurate, but I don't care about that, I'm just using it for show.

    There are many ways to sync your counter to some outside data, but they are beyond the scope of this post, which simply demonstrates how to recreate the animation. You could poll the server every few seconds for fresh data, use websockets for a persistent connect for real-time data, etc. Each use-case depends on what exactly you want to do with the counter.

    As for the random number, just remove the initial increment declaration, and assign it to be a random number within doCount() each time it is called.

  • http://cnanney.com Chris Nanney

    Sorry Sid, this was incorrectly marked as spam. Do you still need help with it?

  • androidmanifest

    Thanks man, i really did it with an AJAX call, and also shrink it, now the problem is to put 2 of these babies on the same page.
    How can i do that?
    Can you make up demo for this?
    Thank you.

  • http://cnanney.com Chris Nanney

    Will the two counters be synced and displaying the same thing, or be totally different?

  • androidmanifest

    Thanks for the quick reply.
    They have to be totally different.

  • Daniel

    I think the whole thing is doable without images at all. CSS 3 with Transitions, Transforms, Rounded Borders, a nice font and a few positioned boxes to cut the digits in two. The mirror filter can probably be achieved easily with an SVG filter applied to the digits or if you don't want to do that, another counter with a mirroring CSS 3 transformation with linear alpha-transparent gradient above it. In other words, I'm pretty sure you can entirely revamp your code preserving ONLY in the javascript the counter itself; in other words, declarative stuff only and a few |digitN++| :-)

    Now, you have something for the next two hours too :-) Best,

    Daniel Glazman
    CSS WG Co-Chairman

  • http://cnanney.com Chris Nanney

    Thanks for commenting, Daniel. I've thought about that before, but you've piqued my curiosity and have given me some motivation to revisit this counter :)

  • Michael Mistal

    Great job with this script. I was wondering if there is an easy or otherwise quick way to re-size the digits? I was looking to add it to a page and basically wanted to cut the size in half.

  • http://cnanney.com Chris Nanney

    Easy, yes. Quick, kinda. The important thing is correctly resizing the image so the animation doesn't jump. The original graphic is 6,180px tall, and 1 of those pixels being off will be noticeable. The target size needs to be divisible by 60, the total number of frames, so there is no rounding going on when resizing. So for the original, 6180 / 60 = 103, and you see in the code var frameShift = 103;.

    So if you wanted half that, say 50px tall, 50 * 60 = 3,000px, the height you'll want to set for the whole graphic when you resize it. Then you need to:

    1) Set new values for the initialPos variable which is just an increment of (frame height * frames per digit) which in this case is (50 * 6), so you get: 0, 300, 600, 900, etc.

    2) Set frameShift = 50.

    3) Update the <li> CSS accordingly.

    That's it. I helped someone do this a little while ago, scaled down about 70% from the original. Take a look. Let me know if you have any problems.

  • http://twitter.com/azee1v1 Azeem Khan

    Which fonts are being used for the digits in the counter?

  • http://cnanney.com Chris Nanney

    Good question, I have no idea. I originally used Apple's graphic and all editing I've done to it has just been resizing and moving digits around. I don't know what the original fonts were.

  • http://twitter.com/azee1v1 Azeem Khan

    Thanks for responding.

  • Chris

    Awesome script, is there a way to make the clock version display in 12HR format instead of as 24HR?

  • http://cnanney.com Chris Nanney

    Sure. Within the getTimeString() function you can do the hour conversion. Some simple code:

    period = hour >= 12 ? 'PM' : 'AM';
    if (hour > 12) hour = hour – 12;

    Then add the AM/PM graphic to the layout and add the logic to switch between them. Would take some work, but not hard to do.

  • Chris

    I don't need the AM/PM graphic so I skipped it but thanks for the code.

  • Cbranch

    Chris, really cool script. I was looking for something like this but was trying to find a way to use it to display how many records were in a mysql table and flip/update as records are added. Have you ever seen any example of this or thought about using your script in that way?

    Thanks!

  • http://cnanney.com Chris Nanney

    Real-time updating would be very complicated, but you could achieve the same thing with polling. Just set an interval on an AJAX request to a script on your server that returns the number of records. However, this style of animation works best with a linear progression of numbers, not jumping around.

    For example, if the number of records at page load is 3, then you poll two seconds later and it's 7, there is no way to smoothly animate from the 3 to the 7. The 3 will animate to a 4, then abruptly jump to 7 at the end of the animation.

    In this situation, if I were going to use this type of counter for such a purpose, I would use longer poll intervals and use an average rate rather than exact number so the animation could be smooth. Maybe poll every 10 seconds, find the difference in # of records, and animate the difference over those ten seconds. So if it goes from 3 to 7, then animate from 3-4-5-6-7 over the following ten seconds. This could get complicated depending on the varying rate of increase, and calculating delay timings and animation speeds, etc.

  • Gunstick

    Hi, I tried to use this for a very slow counter (e.g. 30 digits per minute). Some tests show that at a certain speed, the overflowing from one position to the next is lost. The second digit flips while the first has counted only to 5.
    I tried to get this synced, without luck.
    So in essence the counter is great for high speeds, and for very slow, but in between there's a problem.

  • http://cnanney.com Chris Nanney

    Yes, the slower the pace, the lower the increment has to be. The default values for the counter were created with the purpose of recreating the fast-paced counter on Apple's website. This code isn't a general purpose counting solution, although I've tailored a few different demos for different use cases.

    For a very slow counter like in your example, 1 digit every two seconds, just change the pace to 2000 and the increment to 1, and it will slowly count just fine. The problem is when you'd like the increment to stay high, but animate it slowly, as the animation is currently linear only, e.g. you can't animate from 17 to 39 smoothly in one animation. I made a new version of the script that can animate from any digit to any digit smoothly, so you can slowly count in whatever increment you'd like. I'll probably post about it next week.

  • http://www.blog-nouvelles-technologies.fr/archives/1126/lheure-un-compte-a-rebours-et-un-compteur-a-la-facon-apple/ L’heure, un compte à rebours et un compteur à la façon Apple | Le blog des nouvelles technologies : Web, Technologies, Développement, Interopérabilité

    [...] Source Partager sur Twitter Partager sur Facebook [...]

  • Gunstick

    What I want to do is to count in one minute from one value to another, where the value is arbitrary between 1 and 1000. And count as far as possible with the zeroes falling always together. Is it possible to make a blur by putting several pictures layered one above the other with transparency?

  • yoguyseen

    Hi Chris

    Great work. I tried implementing the countdown clock that you’ve put together and noticed that it does not stay in sync with my system clock. When the counter if first loaded, everything look fine but after a couple of minutes, it is already off by a few seconds. Is it possible that this is due to the fact that “1 second” is defined as 1000ms instead of being tied to the actual time of day? I know it shouldn’t matter because 1s=1000ms but it appears in this case that something doesn’t line up properly.

  • http://cnanney.com Chris Nanney

    Are you talking about the countdown or the clock demo?

  • yoguyseen

    Thanks for your quick reply. I was referring to the countdown clock:

    http://cnanney.com/journal/demo/apple-counter/countdown.php

  • http://cnanney.com Chris Nanney

    All I can say is that it was never meant to be a precise time-keeper, but I don’t know why it is getting off track for you. If you’d like to keep it more precise just always grab the real time to use for the diff every time the doCount function runs.

  • http://www.netsolutions.mv Ammi

    Hi Chris,

    Just came across your script. Modified it a bit and just uploaded it on our site. Works like a charm. Thanks :)

  • Bschippers718

    Anyone know where to find the graphics with a transparent background?

  • http://openid-provider.appspot.com/iambriansreed Your Name

    Thanks. I only noticed it today and then they just took it down. Never seen animation on the web in this way.

  • Fabio Brigazzi

    hi. how can i download the update 4 ? thank you. Fabio (Italy)

  • Anonymous

    Apple-Style Counter
    February 12th, 2010

    Seems this date is off…

  • http://cnanney.com Chris Nanney

    This was inspired by Apple’s 10 billion iTunes song countdown that was back in Feb of last year. They’ve reused the same counting technique a few times since then, most recently for the App Store.

  • Jcoulson

    This is COMPLETELY RAD! I’m using it on our site to build a countdown to launching something. I have only one question: How can I get the event of the countdown ending so I can display another div at that time?

    I tried adding it here, but the clock just resets anyway:

    if (end < now){
    target = 'December 25, 2010';
    end = Date.parse(target);
    }

    Thanks!

    Jeremy

  • http://cnanney.com Chris Nanney

    I updated the script a little. Look inside the doCount() function.

  • Jeremy

    Whoa! That’s perfect. Thanks a ton! Feel free to solve any of the other coding problems I’m tackling in other projects.

    Jeremy

  • Splendiddates

    That’s huge, I was looking for something like
    http://www.badoo.com or
    http://www.cartoonsmart.com/ae_arrivals.html
    and you come with the best answer !!!! jquery + code free !!! Awesome man !!!

  • John

    Hello Chris,

    How can we avoid the reset of the page at every refresh ? Help please…

  • Lyons17

    Maybe someone can help me with an idea I have. I am not sure if this would be the easiest or best way to accomplish what I need. I need a counter like this for a safety board at work. What I would like is if our company paid out $325,000 in accidents last year. I want the counter to count how much money is saved per second or minute or hour working accident free.

    We use a Public view monitor which usually runs a powerpoint of things we want to announce to our employees. Can this be accomplished in Powerpoint or something of the like and can the counter be setup for what I am looking for.

  • http://cnanney.com Chris Nanney

    I don’t understand what you’re asking.

  • http://cnanney.com Chris Nanney

    Powerpoint, no. But you could create a single page to view in a full-screened browser. My revisited counter offers more flexibility, so look at that if you haven’t already. If you figured out the increment rate, the rest is easy.

  • Lyons17

    Awesome! is there a way to not have the counter reset once it starts or the page is refreshed?

  • http://cnanney.com Chris Nanney

    You can alter the script to have it update a url hash of the current value each increment, and then on page load check to see if hash exists and use that as the starting value.

  • Lyons17

    One More question. Can the code be written to include decimal points. In my instance .116 per second

  • Mark

    Chris Can we apply the animation to all the digit as like the airport information board. Like if you we 10000 and increase the value by 1. All the number should have the same animation and the number also should increase.

  • http://cnanney.com Chris Nanney

    Sure it can, but I have no plans to implement that.

  • http://cnanney.com Chris Nanney

    I started playing with a Solari board implementation, but that is better suited for displaying strings of text not a number counter. The point of the counter is to watch a number rapidly increase, and having every digit always animating would be a mess.

  • Arnold

    Hello Chris,

    what do you think about this code here :

    http://stackoverflow.com/questions/4848994/modify-cnanney-apple-flip-counter-to-pick-value-from-joomla-user-database/4921579#4921579

    Can you tweak it please so we can use it elsewhere beside Joomla. Please :(

  • http://cnanney.com Chris Nanney

    It’s sad to see my code turned into such an incoherent rambling mess on stackoverflow. This code is a simple client-side counter implementation. It’s just JavaScript. It’s not Joomla-specific, I’ve never used Joomla in my life. The question seems to be how to get a starting number from the server side to the client side to use the counter.

    Just query the DB and input the number directly to the script server side, or wait and fetch it remotely on the client side.

  • Arnold

    Great, that what I was thinking, and thank you for saving me alot of time because I was going to dive into that messy code.

    I am going to try what you said and hope to get through it. As I read before we should use Ajax…

  • Anonymous

    Great stuff Chris, thx!
    Just changed some dimensions and after a little research on fonts (dafont, apple etc…) came up with Chianti Win95BT (Bold) that looks very very alike, with 1 or two manipulations it’s ok, so if some pple look for it, there you are.
    thanks again
    cheers
    Marcos

  • http://cnanney.com Chris Nanney

    Sweet, looks pretty good. I’ll make a .psd template so all colors, shadows, etc. can be customized.

  • MartinCar

    Very nice work C. Nanney! In your Update4 – countdown demo, how can I add an specific time for a given day. ie (var target = ‘March 23, 2011 , 15:00:00′;). I have a countdown clock that needs to show a live video on March 13, 2011 at 15:00:00. How can I adapt on you countdown script?
    Thanks,
    Strang

  • Dylan

    I tried to get the countdown clock to work on my website but it wont show any of the numbers at all. What am i doing wrong. If posable can you email me the code?

  • http://cnanney.com Chris Nanney

    There is nothing really to email, it’s all there in the demo page. Are you saving copies of the digit background images to your own server and updating the CSS accordingly? Just view the source, save the image files, and update the image path in the CSS if necessary. That’s all you need to do to get it running.

    Does that make sense?

  • http://cnanney.com Chris Nanney

    Well, I have a date demo and a time demo – wouldn’t be too hard to mash them together, I’ve just never had the desire to.

  • Dylan

    I downloaded the file with the all the CSS, Images, JS and the index file then i went to the demo page of the countdown clock and got that code and its not working at all i put the countdown page in the folder with the index page and named it countdown.php and it did not work. All i am getting is “Countdown to…. and the 3 : thats it. I don’t know what else to do.

  • MartinCar

    I think I did not explain correctly. To your countdown work we have to enter the day. e.g. which in my case is March 13, 2011. in addition to that, I need the counter continuing to count until 3pm of March 13, 2011. (var target = ‘March 23, 2011 + 15:00:00′;)
    Thanks

  • http://cnanney.com Chris Nanney

    Oh. It will work as-is, just enter the target date as “March 23, 2011 15:00:00″ and it will work (no plus sign).

  • MartinCar

    You are the man… Thanks.

  • http://cnanney.com Chris Nanney

    Maybe you missed something. Here is everything in a zip file for you, just unpack and open index.html in a browser and it will work.

  • Dylan

    Thanks. Now I have one last question. How can I make the page refresh after the countdown ends?

  • http://cnanney.com Chris Nanney

    You’d have to make some internal changes to the script to have an event fired when the date is reached. Unfortunately the countdown demo was only made to be an example of the animation technique and isn’t very functional or extensible.

    However, with a basic understanding of JavaScript it would not be too tall a task.

  • Anthony Abraira

    This totally rocks! Is it possible to post this as a zipped package. With the demos, CSS, js, etc.

  • Moni

    Hi Chris,
    We’re trying to implement countdown clock to our site but can’t really reproduce the graphics exactly with our own fonts. In one of your past comments you have mentioned a customizable psd file. Is it possible to share it with us so everyone can easily change the counter for their own needs. Thanks in advance!

  • http://cnanney.com Chris Nanney

    Unfortunately this .psd template never came to be. Everything I’ve
    used in the demos to date has been just manipulating Apple’s original
    graphic. I still mean to make an easy to use template, but haven’t
    gotten around to it yet. Sorry.

  • http://www.facebook.com/profile.php?id=100002126675809 Ekambi Alain

    Hello Chris,
    I m currently working on an open source project called gwt4air(http://code.google.com/p/gwt4air/). for the next release of the software i added you counter to the startpage of the explorer application. The project is about a Java API for Adobe FLEX. You can check it out here http://www.gwt4air.appspot.com/. I hope this is OK for you .
    Thanks for your great work.

    Cheers,

    Alain

  • http://cnanney.com Chris Nanney

    Cool. Sizing seems off for me: http://i.imgur.com/cbGUz.png

  • http://www.facebook.com/profile.php?id=100002126675809 Ekambi Alain

    Yeah i know about that issue :) Flex has some problems displaying HTML content, specially when they run inside an Iframe. But i ll will leave it that way for now. Again thanks for your great work.

  • badcode

    Hello Chris,
    Need a page counter that counts visitors, but sets a cookie so they do not get counted twice … is there an easy way to do that?

  • Steve Thorson

    Seemed unnecessary to have three image files so I combined them into one. Here’s the image: http://twitpic.com/4mojnb
    Now you just need to change the three positioning arrays to look like this:
    // 0-9
    var initialPos = [-5562, -4944, -4326, -3708, -3090, -2472, -1854, -1236, -618, 0];
    // 0-5 (first minute and second digit)
    var initialMidPos = [-6180, -4944, -4326, -3708, -3090, -2472];
    // 0-2 (first hour digit)
    var initialSmallPos = [-6798, -4944, -4326];

  • http://cnanney.com Chris Nanney

    Very good point, thanks for posting that.

  • Asdas

    sdasdas

  • Emilien Edmond

    Hello, your tutorial is cool, nice work! But I would adapt the design numbers. Could you send me your PSD source files?
    Thank you

  • http://www.teachingjoomla.com Norm Douglas

    Thanks so much for this… the version I have used has been modified into a Joomla module… but it’s your code.
    I had to modify a few CSS elements, and I’ve combined it with some sifr.
    Here’s the link > http://www.giantcyclingworld.com.au/rockhampton

  • Iain Maclean

    This is a great post, fantastic work. I was wondering if you had any guidance on how I could take a starting number – say 1,000,000 and have it count down to zero based on a set date – say 31st of July 2011? 

    Any help would be much appreciated Chris! Cheers

  • Sensational Fix

    Thanks so much for this post…this is awesome. But, is there any chance to get the strip images with the numbers with transparency background? I mean, instead of the white background… That would be fantastic.
    I hope you can answer my question. Thanks again for sharing your work!

  • http://cnanney.com Chris Nanney

    Someone posted transparent digits for the old style counter here:
    http://cnanney.com/journal/code/apple-style-counter-revisited/#comment-236448317

  • Sensational Fix

    Oh! Thank you sooo much! I have another question if you don’t mind… How can I do to set an specific hour for the countdown? I mean, if I want that the countdown ends at 18,30?
    Thanks so much for your attention. 

  • Sensational Fix

    I’m sorry I’ve found the answer, you are the man!
    Thanks so much!

  • Harry

    The animation for the seconds is a little laggy on my workstation. Normal computer will really get bogged down.

  • Brett

    Did you ever make the psd file by chance?

  • http://cnanney.com Chris Nanney

    Nope.

  • Olivia

    Hi… I opened the filmstrip images in Photoshop, inverted the colors (so the timer is now white and the bg black) and just turned the contrast to 100 to get a crisp white, and VOILA! FTP the new images into your module file >> img and bob is your uncle!

    P.S. To the DEVS – great great great job!

  • http://www.recycledpixels.com/?p=41 Flip Counters & Displays with jQuery & PHP | Recycled Pixels

    [...] But I found a better version. Googling led me to Chris Nanney’s site and his Apple-Style Counter. [...]

  • Ahmet

    I’m initializing counter,but  it’s starting  from zero.

  • Chris

    Love this code!  I’ve spent quite some time looking at other counters and this is by far my favourite.
    I’m actually using the original countdown timer to a specified date.  I’ve noticed that if the window loses focus (e.g. switching tabs), then gains focus, the counter seconds count down very fast until its caught up and is in sync again.

    Is there a way I can make the countdown update/sync instantly when the window gains focus?
    I’ve tried experiementing with jquery e.g.

    $(document).ready(function () {
    $(window).bind(“focus”, function(event)  {
    updateCountdown();
    });    });

    However, browsers are inconsistant in the way they handle blur and focus so I’m not sure if it’s the best approach?

  • http://www.getfound.ie Des Mc Carthy

    love it. can’t wait to try

  • Jacopomichieli

    Doesn’t work with IE 7… Can you fix the bug please?

  • http://internetagency.co.za/apple-style-visitors-counter-for-joomla/ Apple Style Visitors Counter for Joomla | Internet Agency

    [...] Counter by Chris Nanney and available to Joomla by Nima NouriApple-Style Counter is a simple Apple Style module for [...]

blog comments powered by Disqus