My Journey in Open Source - set-interval-by-id

cadienvan

Michael Di Prisco

Posted on August 2, 2023

My Journey in Open Source - set-interval-by-id

What is this?

This is an interval implementation that allows you to set an interval and then clear it using keys instead of the interval variable reference.

This is useful when you have to clear an interval from a different scope than the one where it was set.

How To Install?

npm install set-interval-by-id
Enter fullscreen mode Exit fullscreen mode

How To Use?

import { setIntervalById, clearIntervalById } from 'set-interval-by-id';

const intervalId = setIntervalById(() => {
  console.log('Hello World!');
}, 1000, 'myInterval'); // This will set a interval with the id 'myInterval'

// Meanwhile, in another file...
clearIntervalById('myInterval'); // This will clear the interval
Enter fullscreen mode Exit fullscreen mode

How does it work under the hood?

This library uses a Map to store the interval references, leveraging the singleton pattern.

When you set an interval, it will be stored in the Map with the given id as key.

When you clear an interval, it will be removed from the Map and cleared using the reference stored in the Map.

How to run tests?

npm test
Enter fullscreen mode Exit fullscreen mode

Contributing

If you want to contribute to this project, please open an issue or a pull request.

I will be happy to review it and merge it if it's useful.

Please, remember to follow the Conventional Commits standard.

💖 💪 🙅 🚩
cadienvan
Michael Di Prisco

Posted on August 2, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related