Created
October 15, 2018 07:48
-
-
Save salmanx/0b3b0ad180450b1c976f81b805e15688 to your computer and use it in GitHub Desktop.
code example of setTimeout() and setInterval() for node js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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