timeout-percentage
v1.0.8
Published
Similar to setTimeout, but with multiple intervals to report elapsed time.
Downloads
1,517
Maintainers
Readme
timeout-percentage
Similar to setTimeout
, but periodically reports what percentage of the time has elapsed.
For example, for a timeout value of 1000ms
, you can set 10
intervals, each 100ms
, where you get a callback which reports the elapsed time in percentage. This is useful for creating progress bars.
You can also cancel the timeout before it ends.
Update: Now it can be called multiple times in parallel to setup multiple parallel timeouts.
Install
npm i -s timeout-percentage
Usage
const tp = require("../lib/index.js");
function intervalCallback(percentage) {
console.log(`${percentage}% done.`);
}
function intervalEndCallback() {
console.log(`Interval finished!`);
}
let options = {
intervalCallback: intervalCallback, // Will be called at each interval
intervalEndCallback: intervalEndCallback, // Will be called at the end of the timeout
totalTimeout: 1000, // Total timeout in ms
numberOfIntervals: 10 // Number of intervals
};
tp().start(options);
/*
Output:
10% done.
20% done.
30% done.
40% done.
50% done.
60% done.
70% done.
80% done.
90% done.
100% done.
Interval finished!
*/
See test/test.js for more examples.
Cancelling the timer
You can cancel a timer that is still running:
let myTimer = tp().start(options);
tp().stop(myTimer);