Inserting commas into dynamically generated number

I am using a script that takes a date and time input into the script, and feeds back the total number of seconds from that date and time to the present. The number it feeds back updates every second (adding one as they go by), but the number is one long string of integers with no commas to separate it. How do I insert commas into the number as it updates, so instead of “234567890”, “234567891”, etc…it appears as “234,567,890”, “234,567,891”, etc…? Here below is the script in total (header code and body code). Please tell me how to get this to output the number with the proper commas in place:

<html>
<head>
<script type="text/javascript">
function countup(startingdate, base){
this.currentTime=new Date()
this.startingdate=new Date(startingdate)
this.base=base
this.start()
}
countup.prototype.oncountup=function(){}
countup.prototype.start=function(){
var thisobj=this
this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
var timediff=(this.currentTime-this.startingdate)/1000
var secondfield=Math.floor((timediff))
var result={seconds:secondfield*8466}
this.oncountup(result)
setTimeout(function(){thisobj.start()}, 1000) 
}
</script>
</head>
<body>
<div style="float:left;">$</div><div id="holder"> </div>
<script type="text/javascript">
var startDate=new countup("January 01, 2014 12:00:00", "seconds") // Change starting Date Here
startDate.oncountup=function(result)
{
var mycountainer=document.getElementById("holder")
mycountainer.innerHTML=+result['seconds']
}
</script>
</body>
</html>

How is this?

<html>
<head>
<script type="text/javascript">
function countup(startingdate, base){
this.currentTime=new Date()
this.startingdate=new Date(startingdate)
this.base=base
this.start()
}
countup.prototype.oncountup=function(){}
countup.prototype.start=function(){
var thisobj=this
this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
var timediff=(this.currentTime-this.startingdate)/1000
var secondfield=Math.floor((timediff))
var result={seconds:secondfield*8466}
this.oncountup(result)
setTimeout(function(){thisobj.start()}, 1000) 
}
  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 style="float:left;">$</div><div id="holder"> </div>
<script type="text/javascript">
var startDate=new countup("January 01, 2014 12:00:00", "seconds") // Change starting Date Here
startDate.oncountup=function(result)
{
var mycountainer=document.getElementById("holder")
  var commaFunc=addCommas(result['seconds']);
mycountainer.innerHTML=commaFunc;
}
</script>
</body>
</html>

Works great! Thanks.

No problem. I’m sure that code can be optimized since I just threw in the function (randomly off the Internet) and updated the results array varaible. Looking at the function, it can be optimized. Hopefully a guru can help. I’m a noobie at JS. Stand by.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.