0

I have this code working, but the only drawback is I can't get a comma to appear in the thousands. This is because I am not that good with Javascript. Would someone be kind enough to show me how to do it?

<?php
add_action( 'wp_head', function () { 
?>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $({ countNum: $('.code').html() }).animate({ countNum: 6000 }, {
            duration: 4000,
            easing: 'linear',
            step: function () {
            $('.code').html(Math.floor(this.countNum) + "+");
        },
        complete: function () {
            $('.code').html(this.countNum + "+");
            //alert('finished');
        }
        });
    </script>
    <?php      } );
    ?>
Erçin Dedeoğlu
  • 4,950
  • 4
  • 49
  • 69

1 Answers1

1

You can use regex to format numbers with commas:

   function formatNum(x) {
      return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
   }
skyCode
  • 358
  • 1
  • 7