Счетчик JavaScript постоянно обновляется с определенной скоростью запуска с терминала

Счетчик JS постоянно обновляется

Эй, прежде всего: я новичок в кодировании, я студент, и я так много боролся с этим, что я подумал, что пришло время спросить кого-то еще. Так что я смотрел на этот вопрос, и он отлично работает в 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);

У меня совершенно нет идей, поэтому я был бы очень признателен за любую помощь. Спасибо!

1 ответ

Решение

Почему вы пытаетесь установить количество.innertext в функции обновления, а не только количество? Работает, когда вы используете количество вместо суммы. 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);

Другие вопросы по тегам