{"id":24502967,"url":"https://github.com/reecem/clockworks","last_synced_at":"2025-03-15T08:14:32.357Z","repository":{"id":42806033,"uuid":"270463453","full_name":"ReeceM/clockworks","owner":"ReeceM","description":"Webworker for managing timers and intervals","archived":false,"fork":false,"pushed_at":"2023-01-06T08:16:21.000Z","size":1069,"stargazers_count":1,"open_issues_count":9,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T20:03:31.958Z","etag":null,"topics":["intervals","javascript-library","timers","webworker"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ReeceM.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null}},"created_at":"2020-06-07T23:36:49.000Z","updated_at":"2023-04-02T10:43:51.000Z","dependencies_parsed_at":"2023-02-05T16:46:23.663Z","dependency_job_id":null,"html_url":"https://github.com/ReeceM/clockworks","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReeceM%2Fclockworks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReeceM%2Fclockworks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReeceM%2Fclockworks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ReeceM%2Fclockworks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ReeceM","download_url":"https://codeload.github.com/ReeceM/clockworks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243701463,"owners_count":20333631,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["intervals","javascript-library","timers","webworker"],"created_at":"2025-01-21T23:14:40.226Z","updated_at":"2025-03-15T08:14:32.336Z","avatar_url":"https://github.com/ReeceM.png","language":"JavaScript","funding_links":["https://www.buymeacoffee.com/ReeceM"],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n\u003cbr\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/ReeceM/clockworks/master/docs/clockworks.png\" alt=\"ClockWorks\"\u003e\n  \u003cbr\u003e\n    \u003cbr\u003e\n    ClockWorks\n  \u003cbr\u003e\n\u003c/h1\u003e\n\nWebworker for managing timers and intervals\n\n[![npm version](https://badge.fury.io/js/%40reecem%2Fclockworks.svg)](https://www.npmjs.com/package/@reecem/clockworks)\n[![Total Downloads](https://img.shields.io/npm/dt/@reecem/clockworks.svg )](https://www.npmjs.com/package/@reecem/clockworks)\n![npm bundle size](https://img.shields.io/bundlephobia/minzip/@reecem/clockworks)\n\n\n## Installation\n\nYou can install the package via npm:\n\n```bash\nnpm i @reecem/clockworks\n```\n\n__OR__\n\n```html\n...\n\u003cscript src=\"https://unpkg.com/@reecem/clockworks?umd\"\u003e\u003c/script\u003e\n...\n```\n\n## Usage\n\nWith ClockWorks you can create a new instance of it and specify an array of timers to install in the worker.\n\nEach 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.\n\n### Simple Print to console clock that self terminates\n\n```js\n/**\n * Create a new instance of the class and then print the time to the console.\n *\n * We will also remove the clock after 5 seconds, by counting a variable.\n */\nlet triggers = 0;\nlet clockWorks = new ClockWorks([\n\t{\n\t\tname: 'clock',\n\t\ttime: 1000,\n\t\tcallback: () =\u003e {\n\t\t\tconsole.log((new Date()).toLocaleString())\n\t\t}\n\t},\n\t{\n\t\tname: 'remove-clock',\n\t\ttime: 1000,\n\t\tcallback: () =\u003e {\n\t\t\tif (++triggers \u003e= 5) {\n\t\t\t\t$clock.pull('clock')\n\t\t\t\t$clock.pull('remove-clock')\n\t\t\t}\n\t\t}\n\t}\n])\n```\n\nThe above example will print the time to the terminal, then it will remove itself and the timer printing the time to the console;\n\n### Web Worker\n\nThe 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)\n\n### Timer Definitions\n\nThe 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.\n\n```js\n\n{\n\t/**\n\t * A unique name for the timer being created.\n\t *\n\t * This name is used to track the timer.\n\t */\n\tname: 'Timer',\n\t/**\n\t * The interval of the timer that should be firing in ms\n\t */\n\ttime: 1000,\n\t/**\n\t * The callback function is fired when the timer or interval triggers.\n\t */\n\tcallback: () =\u003e {\n\t\tconsole.log((new Date()).toLocaleString())\n\t}\n}\n```\n\n### Pushing a Single Timer\n\nTo 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.\n\n```js\nconst clockWorks = new ClockWorks();\n\nclockWorks.push({\n\tname: 'new-timer',\n\ttime: 5000,\n\tcallback: () =\u003e {\n\t\tconsole.log('New interval has fired at [%s]', (new Date()).toLocaleString());\n\t}\n})\n```\n\n\u003e ***Important*** An error will be thrown when you try to add a timer with the same name twice to the same instance.\n\n**push** Method\n```js\n\t/**\n\t * Add timers to the list.\n\t *\n\t * @param {Object} timer\n\t * @param {String} timer.name\n\t * @param {Number} timer.time\n\t * @param {Function} timer.callback\n\t *\n\t * @return {Number} the index of the timer on the stack\n\t */\n\tpush(timer)\n```\n\n### Removing a Single Timer Instance\n\nTo remove a timer, you will use the name that you have defined when pushing it onto the timer stack.\n\n```js\nconst clockWorks = new ClockWorks();\n\n// timer that has been defined\nclockWorks.push({ name: 'new-timer', ... })\n\n/**\n * Removing the timer you will use the name that you assigned the timer.\n */\nclockWorks.pull('new-timer');\n```\n\n**pull** Method\n```js\n\t/**\n\t * Remove timer from the stack\n\t *\n\t * @param {String} timer this is the timer name\n\t */\n\tpull(timer)\n```\n\n## Features\n\n- [x] Installable Web Worker bundled\n- [x] Multiple `setIntervals` Definable from the main class on construction\n- [x] Individually add or remove a timer\n- [ ] Have a fallback to the main page thread\n- [ ] Allow defining timeout functions\n- [ ] More definitions for the timers\n- [ ] Hash IDs for the functions and definitions\n- [ ] Improve management of timers.\n- [ ] Cross tab session persistance ??\n\n## Testing\n\nPENDING...\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security\n\nIf 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.\n\n## Credits\n\n- [ReeceM](https://github.com/ReeceM)\n- [All Contributors](../../contributors) (Thanks to any who help)\n\n## Support\n\nI 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.\n\nIf you have the means, a simple coffee would be also appreciated too.\n\n\u003ca href=\"https://www.buymeacoffee.com/ReeceM\" target=\"_blank\"\u003e\u003cimg src=\"https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png\" alt=\"Buy Me A Coffee\" style=\"height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;\" \u003e\u003c/a\u003e\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freecem%2Fclockworks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freecem%2Fclockworks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freecem%2Fclockworks/lists"}