Node

Node Global Timer Simple Web Server Module Postgres DB
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

Node : Timer

2020-04-07

timers.js

Wait for 3 seconds, while showing a percentage update in real time.

const waitTime = 3000;
const waitInterval = 500;
let currentTime = 0;

const incTime = () => {
    currentTime += waitInterval;
    const p = Math.floor((currentTime / waitTime) * 100)
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    process.stdout.write(`Waiting ... ${p}%`)
}

console.log(`Setting a ${waitTime / 1000} second delay`);

const timerFinished = () => {
    clearInterval(interval);
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    console.log("done");
}

const interval = setInterval(incTime, waitInterval);
setTimeout(timerFinished, waitTime);