-2

So this is my code so far:

let name = input.question("Please Provide a Name: ");

if (name === "Casper" || name === "casper"){
  console.log("Access Granted");
  function printMessage(){
    console.log(.);
  }
   setTimeout(printMessage, 1000);
} else {
      console.log("Access Denied");
}

What I want to do is print "..." after Access is Granted, but with each "." printing after a delay, and of course on the same line.

Thanks in advance to any help I get!

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Casper
  • 1
  • You can see this question if you're using node [Node.js console.log - Is it possible to update a line rather than create a new line?](https://stackoverflow.com/questions/17309749/node-js-console-log-is-it-possible-to-update-a-line-rather-than-create-a-new-l) – pilchard May 01 '22 at 21:53
  • For a library see [log-update](https://www.npmjs.com/package/log-update) or, for full blown terminal ui see [blessed](https://github.com/chjj/blessed) – pilchard May 01 '22 at 21:55

1 Answers1

0

You can use setInterval() instead

let 
    counter = 0,
    elipsisInterval = setInterval(() => {
        counter++;
        console.log(`Access Granted${".".repeat(counter)}`);
        if (counter === 3) {
            counter=0;
        }
    }, 1000)
.as-console-wrapper { max-height: 100% !important; top: 0; }

This will print

Access Granted.
Access Granted..
Access Granted...
Access Granted.
Access Granted..
Access Granted...

Each second Because console.log() defaults to end on a line break Apparently if you're on NodeJS (so not on the frontend) you can use

process.stdout.write("Access Granted")
process.stdout.write(".")

and it will stay on the same line

Mushroomator
  • 6,516
  • 1
  • 10
  • 27
dasfacc
  • 148
  • 2
  • 8