I have a function that printing the defined string slowly.
function slowPrint(text, milliseconds) {
var characterID = 0;
var charactersArray = text.split('');
var interval = setInterval(() => {
while (characterID < text.length) {
if (characterID < text.length) {
process.stdout.write(charactersArray[characterID]);
characterID += 1
};
if (characterID == text.length) {
console.log(charactersArray[characterID])
characterID = text.length + 2
};
if (characterID >= parseInt(text.length + 2)) {
break;
};
break;
clearInterval(interval);
};
if (characterID >= parseInt(text.length + 2)) {
clearInterval(interval);
};
}, milliseconds);
};
slowPrint('test 12345', 200);
The code works without any mistakes, but running the function logging the console "undefined" when the printing the string is done.
Output:
test 12345undefined
That undefined output is caused by this line.
console.log(charactersArray[characterID])
If you remove it you should get your expected result.
I have replaced
console.log(charactersArray[characterID])
with
process.stdout.write(charactersArray[characterID]);
process.stdout.write('\n');
and it worked perfectly!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.