单行控制台输出作为变量而不是每个变量值一个console.log?

时间:2021-03-10 13:58:26

Is there means in node.js to advance a counter within the console display to a single line, instead of a console.log output per line?

node.js中有没有办法将控制台显示中的计数器推进到一行,而不是每行的console.log输出?

e.g.

例如

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

prints 10 lines for x = 1 through 10.

为x = 1到10打印10行。

Is it possible instead to have a single line which remains static as the x value increments? As a use case, if the value of x incremented to one million, the console would display the one line until x was 1000000.

是否有可能让一条线在x值增加时保持静止?作为一个用例,如果x的值增加到一百万,控制台将显示一行,直到x为1000000。

1 个解决方案

#1


0  

This does the trick for a single line:

这可以解决一条线:

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

...and then there's this: https://github.com/sindresorhus/log-update

......然后是这个:https://github.com/sindresorhus/log-update

#1


0  

This does the trick for a single line:

这可以解决一条线:

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

...and then there's this: https://github.com/sindresorhus/log-update

......然后是这个:https://github.com/sindresorhus/log-update