JavaScript Countup with commas

I need a countup script with commas similar to what is on sendgrid.com. I have a script which outputs the countup exactly how I need it but without commas. I have found several formatting scripts that will add commas but as I am terrible at javascript I have been unsuccessful at implementing the formatting. Can anyone provide assistance for adding commas to the countup. Any help is greatly appreciated.

Thanks!


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
var START_DATE = new Date("December 1, 2010 20:00:00"); // put in the starting date here
var INTERVAL = 1; // in seconds
var INCREMENT = 1; // increase per tick
var START_VALUE = 4166667; // initial value when it's the start date
var count = 0;

window.onload = function()
{
 var msInterval = INTERVAL * 1000;
 var now = new Date();
 count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
 document.getElementById('counter').innerHTML = count;
 setInterval("count += INCREMENT; document.getElementById('counter').innerHTML = count;", msInterval)
}
</script>
<script>
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\\d+)(\\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
</script>
</head>

<body>
<div id="counter"></div>
</body>
</html>

try this

 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <script type="text/javascript">
 
            var START_DATE = new Date("December 1, 2010 20:00:00"); // put in the starting date here
            var INTERVAL = 1; // in seconds
            var INCREMENT = 1; // increase per tick
            var START_VALUE = 4166667; // initial value when it's the start date
            var count = 0;
 
            window.onload = function() {
                var msInterval = INTERVAL * 1000;
                var now = new Date();
                count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
                document.getElementById('counter').innerHTML = addCommas(count.toString());
                setInterval(incrCount, msInterval);
            }
 
            function incrCount() {
                count += INCREMENT;
                document.getElementById('counter').innerHTML = addCommas(count.toString());
            }
 
            function addCommas(nStr){
                nStr += '';
                x = nStr.split('.');
                x1 = x[0];
                x2 = x.length > 1 ? '.' + x[1] : '';
                var rgx = /(\\d+)(\\d{3})/;
                while (rgx.test(x1)) {
                    x1 = x1.replace(rgx, '$1' + ',' + '$2');
                }
                return x1 + x2;
            }
        </script>
    </head>
    <body>
 
        <div id="counter"></div>
 
    </body>
</html>
 

works perfectly…thanks so much!!

you’re welcome :slight_smile: