Node Timer example

Here is code which will increment a timing function for 3 seconds:

var waitTime = 3000;
var curTime = 0;
var waitInterval = 100;
var percentWait = 0;

function writeWaitingPct(p) {
  process.stdout.clearLine();
  process.stdout.cursorTo(0);
  process.stdout.write(`waiting ... ${p} %`);
}

var interval = setInterval(function () {
  curTime += waitInterval;
  percentWait = Math.floor((curTime/waitTime)*100);
  writeWaitingPct(percentWait);  
}, waitInterval);

setTimeout(function() {
  clearInterval(interval);
  writeWaitingPct(100);
  console.log("\n\nDone!\n");
}, waitTime);

process.stdout.write("\n");
writeWaitingPct(percentWait);

This works with Node JS

They posted on the same topic

Trackback URL : http://codingblog.citytravelling.me.uk/index.php?trackback/9

This post's comments feed