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);

关于javascript - 从终端运行以特定速率连续更新的Javascript计数器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43597609/

10-10 17:48
查看更多