我一直在寻找如何在其他语言中做到这一点,我发现我必须使用特殊字符\b 来删除最后一个字符。 (how-do-i-erase-printed-characters-in-a-console-applicationlinux)
这对 node.js 在多次调用 console.log() 时不起作用;
如果我写一个日志:
console.log ("abc\bd");
我得到结果:abd
但如果我写:
console.log ("abc");
console.log ("\bd");
我得到了结果:
abc
d
我的目标是打印一条等待消息,例如:
等待
等待。
等待..
等待中...
又一次:
等待
等待。
等等
都在同一行。
最佳答案
process.stdout
有可用的函数:
var i = 0; // dots counter
setInterval(function() {
process.stdout.clearLine(); // clear current text
process.stdout.cursorTo(0); // move cursor to beginning of line
i = (i + 1) % 4;
var dots = new Array(i + 1).join(".");
process.stdout.write("Waiting" + dots); // write text
}, 300);
可以为 clearLine(direction, callback)
/**
* -1 - to the left from cursor
* 0 - the entire line // default
* 1 - to the right from cursor
*/
更新 2015 年 12 月 13 日:虽然上述代码有效,但它不再作为 process.stdin
的一部分记录在案。已移至 readline
关于node.js - 如何删除控制台中打印的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11600890/