JQuery animate number counter with commas

Viewed 78

Hello I am trying to auto count or auto increment numbers. I am not sure what is wrong with my jquery. Any suggestions? Thank you!

Number:

<span class="count">6,600</span> <span class="count">600</span> <span class="count">60</span>

JQuery:

    $('.count').each(function () {
      var $this = $(this);
      jQuery({ Counter: 0 }).animate({ Counter: $this.text() }, {
        duration: 1000,
        easing: 'swing',
        step: function () {
          $this.text(commaSeparateNumber(Math.round(this.someValue)));
        }
      });
      function commaSeparateNumber(val){
        while (/(\d+)(\d{3})/.test(val.toString())){
          val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
        }
        return val;
      }
    });
0 Answers
Related