{"id":24847912,"url":"https://github.com/luckydrq/setinterval","last_synced_at":"2025-08-06T10:02:56.764Z","repository":{"id":48440927,"uuid":"77537284","full_name":"luckydrq/setinterval","owner":"luckydrq","description":"More reasonable setInterval for async task","archived":false,"fork":false,"pushed_at":"2021-07-26T10:24:02.000Z","size":11,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-13T01:45:46.669Z","etag":null,"topics":["setinterval","timer","utility"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luckydrq.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-28T14:08:59.000Z","updated_at":"2022-06-13T08:31:37.000Z","dependencies_parsed_at":"2022-09-14T07:01:48.879Z","dependency_job_id":null,"html_url":"https://github.com/luckydrq/setinterval","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Fsetinterval","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Fsetinterval/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Fsetinterval/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luckydrq%2Fsetinterval/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luckydrq","download_url":"https://codeload.github.com/luckydrq/setinterval/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236510483,"owners_count":19160777,"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":["setinterval","timer","utility"],"created_at":"2025-01-31T11:34:56.250Z","updated_at":"2025-01-31T11:34:58.168Z","avatar_url":"https://github.com/luckydrq.png","language":"JavaScript","readme":"# setinterval\n\n[![NPM version][npm-image]][npm-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/setinterval.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/setinterval\n[download-image]: https://img.shields.io/npm/dm/setinterval.svg?style=flat-square\n[download-url]: https://npmjs.org/package/setinterval\n\nWe all know the drawbacks of the built-in `setInterval` in Node.js(actually js itself).\n\nIt's more reasonable to start measuring period after every async task gets done. So here it is.\n\n## Install\n`$ npm i setinterval`\n\n## Example\n```js\n  const Timer = require('setinterval');\n  const t = new Timer(async () =\u003e {\n    const user = await db.User.get(id);\n    console.log(user);\n  }, 1000);\n\n  // start timer\n  t.setInterval();\n\n  // after some time...\n\n  // clear timer\n  t.clearInterval();\n```\n\n## API\n\n### new Timer(fn, period)\n\nTimer constructor.\n\nParams:\n  - fn(*required*): function executed after every `period`. Should be a Promise or async function or generator function or thunk.\n  - period(*required*): timer period(*units: milliseconds*).\n\n### setInterval([initialDelay], [invokeImmediate])\n\nStart timer after a certain delay(defaults to *0*) and can decide if invoke immediately(defaults to *false*).\n\nParams:\n  - initialDelay(*optional*): Delay period(*units: milliseconds*) before timer gets triggered. *default: 0*\n  - invokeImmediate(*optional*): specify if the timer function invoke immediately.*default: false*\n\n### clearInterval()\n\nStop timer(can be restart again).\n\n## Events\n\n### tick\n\nTriggered each time fn is finished, whenever a error is thrown. You can cancel the timer in this event. A `count` parameter is passed in the event handler which stands for how many times fn has been called.\n\n```js\ntimer.on('tick', count =\u003e {\n  timer.clearInterval();\n});\n```\n\n### error\n\nTriggered when error thrown from fn.\n\n```js\ntimer.on('error', e =\u003e {\n  logger.info(e.stack);\n});\n```\n\n## License\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluckydrq%2Fsetinterval","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluckydrq%2Fsetinterval","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluckydrq%2Fsetinterval/lists"}