[ pause-me on npm ]
The "pause-me" utility allows pausing, resuming, stopping and starting a setTimeout
.
$ npm install pause-me --save
$ yarn add pause-me
import pauseMe from "pause-me";
const pauseMe = require("pause-me");
Use it like you would a setTimeout
const myTimeout = pauseMe(() => {
console.log("timed out!");
}, 5000);
or
const myTimeoutFunc = () => {
console.log("timed out!");
};
const myTimeout = pauseMe(myTimeoutFunc, 5000);
As of version 2.0.0, pause-me is written in TypeScript and includes type definitions out of the box:
import pauseMe from "pause-me";
// The returned object is fully typed
const myTimeout = pauseMe(() => {
console.log("timed out!");
}, 5000);
// TypeScript will provide autocomplete and type checking
myTimeout.pause();
Version 2.1.0 introduces two new functions for creating timers and intervals directly:
import { getTimeout, getInterval } from "pause-me";
// Create a timeout directly
const myTimeout = getTimeout(() => {
console.log("Timeout executed!");
}, 5000);
// Create an interval directly
const myInterval = getInterval(() => {
console.log("Interval executed!");
}, 1000);
These functions provide the same interface as the original pauseMe
function but make the intent clearer when you specifically need a timeout or interval.
You can also use it as a setInterval
by setting the repeating
parameter to true
.
let counter = 0;
const myInterval = pauseMe(() => {
counter++;
console.log("Interval " + counter);
}, 5000, true);
Pause the timeout anywhere myTimeout
is in scope.
myTimeout.pause();
Resume the timeout anywhere myTimeout
is in scope.
myTimeout.resume();
Clear the timeout.
myTimeout.stop();
This does not remove myTimeout
from the scope.
Start the timeout from the beginning again.
myTimeout.start();
Restart the timeout at any point.
myTimeout.restart();
Test the setTimeout
instance to see whether or not it is still running.
if (myTimeout.timer() === null) {
// myTimeout is not running
} else {
// myTimeout is running
}
- Added new functions
getTimeout
andgetInterval
for direct timer/interval creation - Improved internal architecture with abstract class implementation
- Enhanced type safety and error handling
- More accurate remaining time calculation
- Better handling of edge cases
- Converted to TypeScript with full type definitions
- Changed to ESM format by default (CommonJS still supported)
- Updated module exports to support both ESM and CommonJS
- Improved error handling and type checking
- Arrow functions used for better
this
binding - Stricter type checking for parameters
Previously, stop
would not do anything if the timeout was paused. This behavior is not intuitive, so now stop
will clear the timeout and reset the timer even if the timeout is paused.
Published under the MIT license.