Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visually count up to number on page load

I have some numbered statistics on my page and I'd like to make them a little more visually appealing. I thought it might be cool to use jQuery to simulate counting up to that number.

So on page load, say the number is 32. The number would start at 0 and count up to 32, with some delay interval between each number.

enter image description here

How would I accomplish this? This is the code that I tried, but it's not working. It captures the number(s) on the page (there are 3 statRibbons), but it's not visually counting up where I can see each number 0, 1, 2, 3, etc. Ideally, it would count up each ribbon one at a time instead of at the same time, but if it requires bulky extra code, then that's okay. The .each should take care of this anyway, I would think.

$(function() {
  $(".statRibbon .bigStat span").each(function() {
      var tmp = this.innerHTML;
      var i = 0;
      while(i != tmp) {
          $(".statRibbon .bigStat span").innerHTML(i++);
          alert(i);
      }
  });
});
like image 698
Jon Avatar asked Jan 03 '13 17:01

Jon


People also ask

How to count page load in jQuery?

See below for the code snippet: var start = new Date(); jQuery. ready(); var end = new Date(); var difference = (endTime - startTime) / 1000; alert("document. ready time: " + difference + " seconds");

How do I count in HTML?

count() method in HTML is used to write the number of times the console. count() method is called. The console. count() method can be added to a label that will be included in the console view.


1 Answers

How does this work for you?

http://jsfiddle.net/WpJxn/1/

Change the 50 at the end of the setTimeout function to change the speed it counts in miliseconds.

like image 72
Adam Merrifield Avatar answered Sep 21 '22 16:09

Adam Merrifield