1

I have a working script that counts up every second from a specified date & time. Could anyone tell me how I could add a commas so it appears something like: 81,547,546

    var START_DATE = new Date("October 10, 2012 22:30:00"); 

    var INTERVAL = 1; 

    var INCREMENT = 1; 

    var START_VALUE = 35001; 

    var count = 0;


    $(document).ready(function() {

     var msInterval = INTERVAL * 1000;

     var now = new Date();

     count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;

     document.getElementById('counter').innerHTML = count;


     window.setInterval( function(){

        count += INCREMENT; 

        document.getElementById('counter').innerHTML = count;

     }, msInterval);

    });
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<div id="counter"></div>
jcubic
  • 61,973
  • 54
  • 229
  • 402
Justin
  • 13
  • 3
  • http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – AmmarCSE May 12 '15 at 12:56
  • But also you're in jquery and it's a solved problem there. I use https://github.com/customd/jquery-number recently and it's been great. – Radio May 12 '15 at 13:20

1 Answers1

1

A bit.. Meh, but hey.. It works. What I did: When it updates I remove the commas from the count. Then I increment it by 1. Then I add the commas and write it on the page. It works, but it's not pretty.

var START_DATE = new Date("October 10, 2012 22:30:00"); 

var INTERVAL = 1; 

var INCREMENT = 1; 

var START_VALUE = 35001; 

var count = 0;


$(document).ready(function() {

  var msInterval = INTERVAL * 1000;

  var now = new Date();

  count = parseInt((now - START_DATE)/msInterval) + START_VALUE;

  document.getElementById('counter').innerHTML = count;


  window.setInterval( function(){
    count = parseFloat(count.toString().replace(/,/g, ''));
    count++; 
    count = count.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    document.getElementById('counter').innerHTML = count;

  }, msInterval);

});
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<div id="counter"></div>
MortenMoulder
  • 6,138
  • 11
  • 60
  • 116