0

I am creating a Discord bot using Node and Discord.js and I want to show the uptime in the console but... I want it to continuously change as time changes.

I have tried and have succeeded in the past of making some loop using process.stdout.write but I can't remember how.

I have tried...

process.stdout.write(process.uptime());
process.stdout.clear();

and it didn't work.

I expect the uptime of the process to show up in the console as...

Uptime: 0D 0H 0M 1S

and as time changes is I want it to update that line and not make another.

1 Answers1

0

You can use setInterval to initiate a loop and update the line periodically:

setInterval(() => {
  process.stdout.cursorTo(0); // Stay on the same line
  let uptime = Math.floor(process.uptime());
  process.stdout.write('Uptime: ' + uptime + 'S');
}, 1000)

I let you make the time conversion in minutes, hours and days for the display.

Credit for cursorTo() method: michelek

TGrif
  • 5,725
  • 9
  • 31
  • 52
  • Works perfectly thank you and [michelek](https://stackoverflow.com/a/37561178/5156280) so much +1. also no worries I know how to do the conversion. – ItsJokerZz May 29 '19 at 01:15