Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

animating from 0 to 100

Hi all I am trying to get some smooth animations going, I want to have 0 and on page load it quickly count up to 100.

how would I do this?

Are you looking at a loading bar or a simple counter?

3 Answers

Hi williamrossi,

See the below fiddle I created quickly, it's a very basic example of using an interval to achieve the desired result, the nicer way of doing this would be to use the requestAnimationFrame callback but for the purposes of this demo setInterval will suffice.

http://jsfiddle.net/tmx8n6zo/4/

Looks ideal! Thanks Chris

No worries, when you replied I had a clear mind and thought about the progress element which overall is much nicer to use but of course carries an IE10+ and modern browser restriction.

http://jsfiddle.net/7ko7gsxk/2/

Thanks again I would not be able to write something like this from scratch, I can read it just about and I understand what it is doing but could you tell me a few things..

what are the [0] 's doing, is that picking the first 'div' and 'span' it finds? and what is the 2000 / 100 part? is that just the timing?

and also is there a way to generate random numbers first? I have a another screen where I want to have a number generate anything between 0-500 and after about 4 random numbers it lands on 500 and stays there are a few seconds.

thanks again