setinterval-bykey
v1.0.3
Published
A simple Node.js utility to manage multiple named intervals easily.
Maintainers
Readme
Interval Manager
- A lightweight and easy-to-use Node.js utility to manage multiple named intervals.
Features
- Start multiple intervals identified by unique keys.
- Check if a specific interval is running by its key.
- Clear individual intervals or all intervals at once.
- Throws clear errors on invalid input.
- Minimal dependencies, pure Node.js timer management.
Installation
npm install setinterval-bykeyUsage
const intervalManager = require('interval-manager');
// Start an interval that logs "Hello" every second
intervalManager.start(() => {
console.log('Hello');
}, 1000, 'greeting');
// Check if 'greeting' interval is running
console.log(intervalManager.isRunning('greeting')); // true
// Stop the 'greeting' interval after 5 seconds
setTimeout(() => {
intervalManager.clear('greeting');
console.log('Greeting interval stopped');
}, 5000);API
start(fn, interval, key)
Starts a new interval with the callback function fn running every interval milliseconds under the identifier key.
Throws TypeError if parameters are invalid.
Returns true if interval started successfully, or false if an interval with the same key already exists.
isRunning(key)
Checks if an interval under the given key is currently active.
Throws TypeError if key is invalid.
Returns true if running, otherwise false.
clear(key)
Stops and removes the interval identified by key.
Throws TypeError if key is invalid.
Returns true if interval was cleared, or false if no interval found under that key.
clearAll()
Stops and clears all running intervals.
📜 License
MIT
✍️ Author
Youssef Mohamed - (SLASH)
🔗 Links
Feel free to open issues or contribute! 🚧
