2017-09-30 7 views
0

node.jsには、console.logを1行に出力するのではなく、コンソール表示内のカウンタを1行に進める手段がありますか?変数値として1つのconsole.logではなく、1行のコンソール出力が変数として変更されますか?

let x = 1 
 
while (x <= 10) { 
 
    console.log(`The value assigned to 'x' is now: ${x}`); 
 
    x++; 
 
    }

介してプリントX = 1の10行をx値の増分として静止したまま単一ラインを有する代わりに、それは可能ですか? xは1000000

答えて

0

になるまでのユースケースとしては、1百万インクリメントxの値ならば、コンソールが1行を表示していましたこれは、単一の行のためのトリックを行います。

const process = require('process'); 
let x = 1; 
process.stdout.write(`The count is now at: ${x}`); 
const counter =() => { 
    process.stdout.clearLine(); 
    process.stdout.cursorTo(0); 
    x++; 
    process.stdout.write(`The count is now at: ${x}`); 
    if(x >= 10) { 
    clearInterval(cycle); 
    process.stdout.write('\n'); 
    } 
}; 

const cycle = setInterval(counter, 1000); 

を...次にこれがあります:https://github.com/sindresorhus/log-update