在几秒钟内为日期自动收报机添加逗号

时间:2015-05-12 12:54:31

标签: javascript datetime

我有一个工作脚本,从指定的日期开始每秒计算一次。时间。谁能告诉我如何添加一个逗号,所以它看起来像是: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>

1 个答案:

答案 0 :(得分:0)

有点......呃,但是嘿..它有效。我做了什么:当它更新时,我从计数中删除逗号。然后我将它递增1.然后我添加逗号并将其写在页面上。它有效,但它并不漂亮。

$url = 'https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=ferrariworld&key=ZZZZZZZZ';
$json = json_decode(file_get_contents($url), true);
foreach ($json as $item){
echo $item[0]['id'];
echo $item[0]['title'];
}
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);

});