Javascript 计数器从终端以特定速率 运行 持续更新

Javascript counter continuously updating with specific rate run from terminal

JS counter continuously updating

嘿,首先:我是编码的新手,我是一名学生,我一直在努力解决这个问题,所以我认为终于是时候问问别人了。所以我一直在查看 this question,它在 html 中完美运行。但是我想从终端 运行 它(我正在使用节点来执行文件)。 我试图为此修改代码。但计算似乎不起作用。它打印:£NaN.00

//counter
var amount = formatMoney(amount);
var startDate = new Date(2012, 07, 21);
var currentDate = new Date();
var seconds = (startDate - currentDate) / 1000;
var modifier = seconds * .158;
var current = 138276343 + modifier;

update();

function update() {
    amount.innerText = formatMoney(current);
}

setInterval(function(){
    current += .158;
    update();
},1000);

function formatMoney(amount) {
    var pounds = Math.floor(amount).toString().split('');
    var cents = (Math.round((amount%1)*100)/100).toString().split('.')[1];
    if(typeof cents == 'undefined'){
        cents = '00';
    }else if(cents.length == 1){
        cents = cents + '0';
    }
    var str = '';
    for(i=pounds.length-1; i>=0; i--){
        str += pounds.splice(0,1);
        if(i%3 == 0 && i != 0) str += ',';
    }
    return '£' + str + '.' + cents;
}

console.log(amount);

我完全没有想法,所以我真的很感激任何帮助。谢谢!

为什么您尝试在更新函数中设置 amount.innertext 而不仅仅是数量?当您使用 amount 而不是 amount.innertext.

时有效

//counter
var amount = formatMoney(amount);
var startDate = new Date(2012, 07, 21);
var currentDate = new Date();
var seconds = (startDate - currentDate) / 1000;
var modifier = seconds * .158;
var current = 138276343 + modifier;
update();
function update() {
    amount = formatMoney(current);
}
setInterval(function(){
    current += .158;
    update();
},1000);
function formatMoney(amount) {
    var pounds = Math.floor(amount).toString().split('');
    var cents = (Math.round((amount%1)*100)/100).toString().split('.')[1];
    if(typeof cents == 'undefined'){
        cents = '00';
    }else if(cents.length == 1){
        cents = cents + '0';
    }
    var str = '';
    for (i=pounds.length-1; i>=0; i--) {
        str += pounds.splice(0,1);
        if(i%3 == 0 && i != 0) {
            str += ",";
        }
    }
    return '£' + str + '.' + cents;
}
console.log(amount);