{"id":17949724,"url":"https://github.com/slurmulon/dynamic-interval","last_synced_at":"2025-03-24T23:32:02.735Z","repository":{"id":57217680,"uuid":"91292367","full_name":"slurmulon/dynamic-interval","owner":"slurmulon","description":":clock1: The dynamic setInterval","archived":false,"fork":false,"pushed_at":"2019-02-18T06:48:44.000Z","size":121,"stargazers_count":4,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-19T05:23:15.280Z","etag":null,"topics":["clock","interval","iteration","setinterval","step","time","wait"],"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/slurmulon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-15T03:40:51.000Z","updated_at":"2023-08-17T22:39:56.000Z","dependencies_parsed_at":"2022-08-28T21:41:26.700Z","dependency_job_id":null,"html_url":"https://github.com/slurmulon/dynamic-interval","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slurmulon%2Fdynamic-interval","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slurmulon%2Fdynamic-interval/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slurmulon%2Fdynamic-interval/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slurmulon%2Fdynamic-interval/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slurmulon","download_url":"https://codeload.github.com/slurmulon/dynamic-interval/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245372193,"owners_count":20604487,"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":["clock","interval","iteration","setinterval","step","time","wait"],"created_at":"2024-10-29T09:32:19.557Z","updated_at":"2025-03-24T23:32:02.481Z","avatar_url":"https://github.com/slurmulon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dynamic-interval\n\n\u003e :clock1: The dynamic setInterval\n\n---\n\n[![build](https://img.shields.io/circleci/project/github/RedSparr0w/node-csgo-parser.svg?style=for-the-badge)](https://circleci.com/gh/slurmulon/dynamic-interval)\n[![npm](https://img.shields.io/npm/v/dynamic-interval.svg?style=for-the-badge)](https://npmjs.com/package/dynamic-interval)\n\n`setInterval` with the ability to specify a new interval duration on each tick.\n\nAlso referred to as a \"dynterval\".\n\n## Sections\n\n- [Install](#install)\n- [Usage](#usage)\n- [Examples](#examples)\n  * [Basic](#basic)\n  * [Advanced](#advanced)\n- [Interface](#interface)\n- [Related](#related)\n- [License](#license)\n\n## Install\n\n```sh\nnpm install dynamic-interval\n```\n\n## Usage\n\n```js\nimport setDynterval from 'dynamic-interval'\n\nconst action = context =\u003e console.log('tick!', context)\nconst wait = 100\n\nconst dynterval = setDynterval(action, wait)\n\n// tick! { wait: 100 }\n```\n\n## Examples\n\n### Basic\n\nThis script doubles the duration of the interval on each iteration, starting with 50ms:\n\n```js\nimport setDynterval from 'dynamic-interval'\n\n// you can attach arbitrary properties to this object (in this case, `rate`), but\n// `wait` is what's used to determine the duration between each interval\nconst config = { wait: 50, rate: 2 }\n\nconst dynterval = setDynterval(context =\u003e {\n  console.log('interval', context)\n\n  const next = context.wait * context.rate\n\n  return { ...context, wait: next }\n}, config)\n\n// interval { wait: 50,  rate: 2 }\n// interval { wait: 100, rate: 2 }\n// interval { wait: 200, rate: 2 }\n// ...\n\n// clear out the interval after 2 seconds\n// NOTE: `window.clearInterval` is not compatible! use the `clear` method instead\nsetTimeout(() =\u003e {\n  dynterval.clear()\n}, 2000)\n```\n\n### Advanced\n\nThis script calculates the amount of drift on each step and corrects for it during the subsequent step.\n\nIt uses a custom interval `api`. In this case, we're using [`worker-timers`](https://www.npmjs.com/package/worker-timers).\n\n```js\nimport setDynterval from 'dynamic-interval'\nimport * as workerTimers from 'worker-timers'\n\nconst setAccurateInterval = (func, wait) =\u003e {\n  let expected = Date.now() + wait\n\n  return setDynterval(context =\u003e {\n    const drift = Date.now() - expected\n\n    if (drift \u003e wait)\n      throw Error(`that drift be crazy: ${drift}`)\n\n    expected += wait\n\n    const next = Math.max(0, wait - drift)\n\n    func(context)\n\n    return { ...context, drift, wait: next }\n  }, wait, workerTimers)\n}\n\nsetAccurateInterval(context =\u003e console.log('tick', context), 1000)\n```\n\n## Interface\n\n### ```setDynterval(\u003caction\u003e, \u003cwait|config\u003e, \u003capi\u003e)```\n\n#### `action`\n\nThe callback to invoke on each interval tick\n\n- **Type**: `Function`\n- **Required**\n\n#### `wait`\n\nSpecifies the duration of each interval (i.e. the amount of time to wait between each tick)\n\n- **Type**: `Number`\n\n\n#### `config`\n\nSpecifies the configuration of the interval. Passed into the `action` function as `context`.\n\n- **Type**: `Object`\n\n- **Properties**:\n\n  * **`wait`**\n\n    Specifies the duration of each interval\n\n    - **Type**: `Number`\n\n  * **`immediate`**\n\n    Determines if the interval should start immediately or wait one interval before starting\n\n    - **Type**: `Boolean`\n    - **Default**: `false`\n\n#### `api`\n\nA custom interval `api` may be provided. It must define functions for either `setInterval` and `clearInterval` or `setTimeout` and `clearTimeout`.\n\n - **Type**: `Object`\n\n - **Properties**:\n\n   * **`setTimeout`**\n\n     Defines how to create a new timeout\n\n     - **Type**: `Function`\n     - **Signature**: `setTimeout(func: Function, delay: Number)`\n     - **Returns**: `TimeoutID`\n     - **Default**: [`WindowOrWorkerGlobalScope.setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout)\n\n   * **`clearTimeout`**\n\n     Defines how to clear or cancel a timeout\n\n     - **Type**: `Function`\n     - **Signature**: `clearTimeout(id: TimeoutID)`\n     - **Returns**: `void`\n     - **Default**: [`WindowOrWorkerGlobalScope.clearTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout)\n\n   * **`setInterval`**\n\n     Defines how to create a new interval\n\n     - **Type**: `Function`\n     - **Signature**: `setInterval(func: Function, delay: Number)`\n     - **Returns**: `IntervalID`\n     - **Default**: [`WindowOrWorkerGlobalScope.setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval)\n\n   * **`clearInterval`**\n\n     Defines how to clear or cancel an interval\n\n     - **Type**: `Function`\n     - **Signature**: `clearInterval(id: IntervalID)`\n     - **Returns**: `void`\n     - **Default**: [`WindowOrWorkerGlobalScope.clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval)\n\n\n## Related\n\n- [`stateful-dynamic-interval`](https://github.com/slurmulon/stateful-dynamic-interval) adds pause, resume and grouping functionality to `dynamic-interval`.\n- [`accurate-interval`](https://npmjs.com/accurate-interval) an interval that automatically corrects for local drift on each tick. May be provided as an `api`.\n- [`audio-context-timers`](https://npmjs.com/audio-context-timers) an interval that uses the Web Audio API clock. May be provided as an `api`.\n- [`worker-timers`](https://npmjs.com/worker-timers) an interval that uses Service Workers as a backend. May be provided as an `api`.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslurmulon%2Fdynamic-interval","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslurmulon%2Fdynamic-interval","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslurmulon%2Fdynamic-interval/lists"}