Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created October 15, 2018 07:48
Show Gist options
  • Save salmanx/0b3b0ad180450b1c976f81b805e15688 to your computer and use it in GitHub Desktop.
Save salmanx/0b3b0ad180450b1c976f81b805e15688 to your computer and use it in GitHub Desktop.
code example of setTimeout() and setInterval() for node js
var readline = require('readline');
var waitTime = 3000;
var currentTime = 0;
var waitInterval = 500;
var percentageWaited = 0;
function writeWaitingPercentage(p){
readline.clearLine(process.stdout);
readline.cursorTo(process.stdout, 0)
process.stdout.write(`waiting for.....${p}%`);
}
var interval = setInterval(function(){
currentTime += waitInterval;
// console.log(`You are waiting ${currentTime/1000} seconds`);
percentageWaited = Math.floor((currentTime/waitInterval) * 100);
writeWaitingPercentage(percentageWaited);
}, waitInterval);
setTimeout(function(){
console.log("\n\nDone\n\n");
clearInterval(interval);
}, waitTime);
writeWaitingPercentage(percentageWaited);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment