pause-me
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

Dynamic JSON Badge Static Badge install size Dynamic JSON Badge Node.js CI

pause-me, A Dependency Free setTimeout Utility

[ pause-me on npm ]

The "pause-me" utility allows pausing, resuming, stopping and starting a setTimeout.

Install

$ npm install pause-me --save  
$ yarn add pause-me

Usage

ESM Import (Recommended)

import pauseMe from "pause-me";

CommonJS Require

const pauseMe = require("pause-me");

Basic Usage

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

TypeScript Support

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

New in v2.1.0: Direct Timer and Interval Creation

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.

Interval Mode

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

API

pause

Pause the timeout anywhere myTimeout is in scope.

myTimeout.pause();  

resume

Resume the timeout anywhere myTimeout is in scope.

myTimeout.resume();  

stop

Clear the timeout.

myTimeout.stop();  

This does not remove myTimeout from the scope.

start

Start the timeout from the beginning again.

myTimeout.start();  

restart

Restart the timeout at any point.

myTimeout.restart();

timer

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
}

Breaking Changes

Version 2.1.0

  • Added new functions getTimeout and getInterval 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

Version 2.0.0

  • 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

Version 1.3.0

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.

License

Published under the MIT license.

Package Sidebar

Install

npm i pause-me

Weekly Downloads

88

Version

2.1.0

License

MIT

Unpacked Size

23.5 kB

Total Files

10

Last publish

Collaborators

  • jpehman