{"id":32525699,"url":"https://github.com/parzh/recursive-timeout","last_synced_at":"2025-10-28T09:52:25.757Z","repository":{"id":318879716,"uuid":"1074393779","full_name":"parzh/recursive-timeout","owner":"parzh","description":"`setInterval` implemented as a recursive `setTimeout`","archived":false,"fork":false,"pushed_at":"2025-10-15T14:58:32.000Z","size":65,"stargazers_count":1,"open_issues_count":3,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-16T04:48:55.441Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.org/package/recursive-timeout","language":"TypeScript","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/parzh.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-10-11T17:47:08.000Z","updated_at":"2025-10-15T14:57:00.000Z","dependencies_parsed_at":"2025-10-17T01:06:07.034Z","dependency_job_id":"1d7414c3-aa2a-4ea8-99b8-ffaf6ad61403","html_url":"https://github.com/parzh/recursive-timeout","commit_stats":null,"previous_names":["parzh/recursive-timeout"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/parzh/recursive-timeout","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Frecursive-timeout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Frecursive-timeout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Frecursive-timeout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Frecursive-timeout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parzh","download_url":"https://codeload.github.com/parzh/recursive-timeout/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parzh%2Frecursive-timeout/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281418064,"owners_count":26497723,"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","status":"online","status_checked_at":"2025-10-28T02:00:06.022Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-10-28T09:52:01.495Z","updated_at":"2025-10-28T09:52:25.752Z","avatar_url":"https://github.com/parzh.png","language":"TypeScript","readme":"# `recursive-timeout`\n\n[![npm version](https://badge.fury.io/js/recursive-timeout.svg)](https://badge.fury.io/js/recursive-timeout)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA simple solution to a classic problem: `setInterval` implemented as recursive `setTimeout` calls.\n\nThis package is designed to be a drop-in replacement for the normal `setInterval`.\n\n## \"Why exactly do I need this?\"\n\nTake a look at [this section from javascript.info](https://javascript.info/settimeout-setinterval#nested-settimeout), it outlines the problem and the solution.\n\nBasically, the normal `setInterval` doesn't wait for the callback to happen, – it schedules the next iteration right away:\n\n```js\nfunction runsForOneSecond() {\n  const startTime = Date.now()\n\n  while (Date.now() - startTime \u003c 1000) { /* just wait */ }\n\n  console.log(Date.now(), 'Done!')\n}\n\nconsole.log(Date.now(), 'Start')\nsetInterval(runsForOneSecond, 500)\n// logs (approximately):\n// 1234567890000 Start\n// 1234567891500 Done!\n// 1234567892500 Done!\n// 1234567893500 Done!\n```\n\n… while the \"recursive timeout\" approach necessarily waits for the callback to finish before scheduling the next call:\n\n```js\nimport { setRecursive } from 'recursive-timeout'\n\nconsole.log(Date.now(), 'Start')\nsetRecursive(runsForOneSecond, 500)\n// logs (approximately):\n// 1234567890000 Start\n// 1234567891500 Done!\n// 1234567893000 Done!\n// 1234567894500 Done!\n```\n\n## Features\n\n- **Dual Module Support:** Works seamlessly with both ECMAScript Modules (`import`) and CommonJS (`require`).\n- **Familiar API:** Designed as a drop-in replacement for `setInterval`.\n- **Promise-based API:** ⚠️ _(coming soon)_ ⚠️ A promise-based interface for use with asynchronous callbacks.\n\n## Installation\n\n```bash\nnpm install recursive-timeout\n```\n\n## Usage\n\nThe straightforward API of `setRecursive` and `clearRecursive` mirrors the native `setInterval` and `clearInterval` functions.\n\n#### ECMAScript\n\nThis is the standard way to use the package, ideal for most cases.\n\n```js\nimport { setRecursive, clearRecursive } from 'recursive-timeout'\n\nsetRecursive(() =\u003e console.log('hi'), 1000)\nsetRecursive(console.log, 1000, 'hi')\n```\n\nTo cancel the interval, pass the timer into `clearRecursive` or the standard `clearInterval`, or even `clearTimeout`:\n\n```js\nconst recursive = setRecursive(console.log, 1000, 'hi')\n\nclearRecursive(recursive)\nclearInterval(recursive) // ✅ works\nclearTimeout(recursive) // ✅ also works\n```\n\nFor TypeScript users, some additional checks are added:\n\n```ts\nfunction sum(a: number, b: number): void {\n  console.log(a + b)\n}\n\nsetRecursive(sum, 100)\n// ❌ Error: not enough arguments\n\nsetRecursive(sum, 100, 42)\n// ❌ Error: not enough arguments\n\nsetRecursive(sum, 100, 42, 17)\n// ✅ OK (logs 59 every ~100 milliseconds)\n```\n\n#### ECMAScript (promise-based) – _coming soon_ ⚠️\n\n```js\nimport { setRecursive, clearRecursive } from 'recursive-timeout/promises'\n```\n\n```js\nimport { promises } from 'recursive-timeout'\n\npromises.setRecursive(…)\npromises.clearRecursive(…)\n```\n\n#### CommonJS\n\n```js\nconst { setRecursive, clearRecursive } = require('recursive-timeout')\n```\n\n#### CommonJS (promise-based) – _coming soon_ ⚠️\n\n```js\nconst { setRecursive, clearRecursive } = require('recursive-timeout/promises')\n```\n\n```js\nconst { promises } = require('recursive-timeout')\n\npromises.setRecursive(…)\npromises.clearRecursive(…)\n```\n\n## License\n\nThis project is licensed under the MIT License.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparzh%2Frecursive-timeout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparzh%2Frecursive-timeout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparzh%2Frecursive-timeout/lists"}