1.0.8 • Published 6 years ago

timeout-percentage v1.0.8

Weekly downloads
420
License
ISC
Repository
github
Last release
6 years ago

timeout-percentage

NPM NPM Downloads

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);
1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago