https://github.com/reecem/clockworks
Webworker for managing timers and intervals
https://github.com/reecem/clockworks
intervals javascript-library timers webworker
Last synced: 7 months ago
JSON representation
Webworker for managing timers and intervals
- Host: GitHub
- URL: https://github.com/reecem/clockworks
- Owner: ReeceM
- License: mit
- Created: 2020-06-07T23:36:49.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T08:16:21.000Z (almost 3 years ago)
- Last Synced: 2025-03-01T20:03:31.958Z (7 months ago)
- Topics: intervals, javascript-library, timers, webworker
- Language: JavaScript
- Homepage:
- Size: 1.02 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Security: SECURITY.md
Awesome Lists containing this project
README
![]()
ClockWorks
Webworker for managing timers and intervals
[](https://www.npmjs.com/package/@reecem/clockworks)
[](https://www.npmjs.com/package/@reecem/clockworks)
## Installation
You can install the package via npm:
```bash
npm i @reecem/clockworks
```__OR__
```html
......
```## Usage
With ClockWorks you can create a new instance of it and specify an array of timers to install in the worker.
Each timer has a set of define values as an object and a callback. These definitions can be added at when instantiating the class or via the `push`/`pull` methods on the class once it has been created.
### Simple Print to console clock that self terminates
```js
/**
* Create a new instance of the class and then print the time to the console.
*
* We will also remove the clock after 5 seconds, by counting a variable.
*/
let triggers = 0;
let clockWorks = new ClockWorks([
{
name: 'clock',
time: 1000,
callback: () => {
console.log((new Date()).toLocaleString())
}
},
{
name: 'remove-clock',
time: 1000,
callback: () => {
if (++triggers >= 5) {
$clock.pull('clock')
$clock.pull('remove-clock')
}
}
}
])
```The above example will print the time to the terminal, then it will remove itself and the timer printing the time to the console;
### Web Worker
The package installs it's own Web Worker that has been bundled, so there is no need to worry about the specifics of the web worker or it conflicting with other workers that you may have on the webpage. See it here [worker.js](./src/worker.js)
### Timer Definitions
The ClockWorks library takes a standard style of interval or timer definition, this allows it to track them to be able to clear them or add them.
```js
{
/**
* A unique name for the timer being created.
*
* This name is used to track the timer.
*/
name: 'Timer',
/**
* The interval of the timer that should be firing in ms
*/
time: 1000,
/**
* The callback function is fired when the timer or interval triggers.
*/
callback: () => {
console.log((new Date()).toLocaleString())
}
}
```### Pushing a Single Timer
To add a single timer you will use the instance of the class that you have created and call the `push` method with a timer object.
```js
const clockWorks = new ClockWorks();clockWorks.push({
name: 'new-timer',
time: 5000,
callback: () => {
console.log('New interval has fired at [%s]', (new Date()).toLocaleString());
}
})
```> ***Important*** An error will be thrown when you try to add a timer with the same name twice to the same instance.
**push** Method
```js
/**
* Add timers to the list.
*
* @param {Object} timer
* @param {String} timer.name
* @param {Number} timer.time
* @param {Function} timer.callback
*
* @return {Number} the index of the timer on the stack
*/
push(timer)
```### Removing a Single Timer Instance
To remove a timer, you will use the name that you have defined when pushing it onto the timer stack.
```js
const clockWorks = new ClockWorks();// timer that has been defined
clockWorks.push({ name: 'new-timer', ... })/**
* Removing the timer you will use the name that you assigned the timer.
*/
clockWorks.pull('new-timer');
```**pull** Method
```js
/**
* Remove timer from the stack
*
* @param {String} timer this is the timer name
*/
pull(timer)
```## Features
- [x] Installable Web Worker bundled
- [x] Multiple `setIntervals` Definable from the main class on construction
- [x] Individually add or remove a timer
- [ ] Have a fallback to the main page thread
- [ ] Allow defining timeout functions
- [ ] More definitions for the timers
- [ ] Hash IDs for the functions and definitions
- [ ] Improve management of timers.
- [ ] Cross tab session persistance ??## Testing
PENDING...
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Security
If you discover any security related issues, please email zsh.rce@gmail.com instead of using the issue tracker. You can also use the [SECURITY](SECURITY.md) doc.
## Credits
- [ReeceM](https://github.com/ReeceM)
- [All Contributors](../../contributors) (Thanks to any who help)## Support
I enjoy building things and making all manner of programs and helping in open-source projects. If it has been really useful to you and you appreciate it you can leave a star on the repo.
If you have the means, a simple coffee would be also appreciated too.
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.