The code below counts 2 seconds from 0 to the specified number. How to make it count numbers from 0 to 12,456 - that is, return a number with commas?

var currentNumber = 0; var simple = '<?php echo $g_kill; ?>'; $({numberValue: currentNumber}).animate({numberValue: simple}, { duration: 2000, step: function() { $('.fot-global-txt').text(Math.ceil(this.numberValue)); } }); 

Displays NaN

  • It if var = 12,234,123 then it turns out NaN. - Sauron
  • How can I fix it? - Sauron
  • Jean I want to make it rotate the number 12.345, that is, from zero goes to the number 12.345.
  • @Sauron you on the contrary need to get rid of both commas and points in the simple .... - Alexey Shimansky

1 answer 1

 var currentNumber = 0; var simple = '30000'; var $el = $('.fot-global-txt'); $({numberValue: currentNumber}).animate({numberValue: simple}, { duration: 2000, step: function() { var v = Math.ceil(this.numberValue) .toString() .replace(/\B(?=(\d{3})+(?!\d))/g, ",") ; $el.text(v); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="fot-global-txt"></div>