我一直在搜索如何用其他语言执行此操作,发现必须使用特殊字符 \b 来删除最后一个字符。 (如何删除控制台应用程序中打印的字符 linux https://stackoverflow.com/questions/430713/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
我的目标是打印一条等待消息,例如:
Waiting
等待。
等待..
等待...
然后再次:
Waiting
等待。
etc
都在同一条线上。
有可用的功能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
*/
Update2015 年 12 月 13 日:虽然上面的代码可以工作,但它不再被记录为process.stdin
。它已移至readline https://nodejs.org/api/readline.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)