https://github.com/ftonato/set-interval-manager
A simple utility for managing intervals without having to manually track interval IDs. Easily start and stop intervals using a unique identifier.
https://github.com/ftonato/set-interval-manager
clearinterval interval interval-manager setinterval timer
Last synced: 3 months ago
JSON representation
A simple utility for managing intervals without having to manually track interval IDs. Easily start and stop intervals using a unique identifier.
- Host: GitHub
- URL: https://github.com/ftonato/set-interval-manager
- Owner: ftonato
- License: mit
- Created: 2023-04-28T22:48:32.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-20T01:43:51.000Z (about 2 years ago)
- Last Synced: 2025-02-09T05:27:28.551Z (3 months ago)
- Topics: clearinterval, interval, interval-manager, setinterval, timer
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/set-interval-manager
- Size: 167 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# set-interval-manager
[](https://www.npmjs.com/package/set-interval-manager)
[](https://github.com/ftonato/set-interval-manager/blob/main/LICENSE)A utility class for managing intervals created by `setInterval`.
## Installation
You can install the package using npm:
```bash
npm install set-interval-manager
```## Examples
```js
// ES6 import
import SetInterval from 'set-interval-manager';// (or) CommonJS require
const SetInterval = require('set-interval-manager');const mockFn = () => 'set-interval-manager';
// --- (start + clear) ---
SetInterval.start(mockFn, 1000, 'basic-example');
SetInterval.clear('basic-example');
// --- (start + clearAll) ---
SetInterval.start(mockFn, 1000, 'interval-one');
SetInterval.start(mockFn, 1500, 'interval-two');
SetInterval.start(mockFn, 2000, 'interval-three');SetInterval.clearAll();
// --- (start + listAll) ---
SetInterval.start(mockFn, 1000, 'interval-one');
SetInterval.start(mockFn, 1500, 'interval-two');SetInterval.listAll(); // => ['interval-one', 'interval-two']
SetInterval.clear('interval-one');
SetInterval.listAll(); // => ['interval-two']
```----
## API
### `SetInterval`
The main class exported by the package.
#### `start(fn: IntervalFn, interval: number, key: string): void`
Starts a new interval that calls the specified function at the specified interval.
- `fn` (required): The function to call.
- `interval` (required): The interval (in milliseconds) at which to call the function.
- `key` (required): A unique string identifier for the interval.#### `clear(key: string): void`
Stops the interval with the specified key.
- `key` (required): The string identifier for the interval to stop.
#### `clearAll(): void`
Stops all intervals managed by this utility.
#### `listAll(): string[]`
Gets an array of all keys currently being used to manage intervals.
----
## License
This package is released under the [MIT License](https://github.com/ftonato/set-interval-manager/blob/main/LICENSE).