{"id":15314232,"url":"https://github.com/barelyhuman/background-timer","last_synced_at":"2025-04-15T01:44:53.411Z","repository":{"id":101912940,"uuid":"317638183","full_name":"barelyhuman/background-timer","owner":"barelyhuman","description":"React hook to run a timer in the background","archived":false,"fork":false,"pushed_at":"2020-12-29T13:34:56.000Z","size":437,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T13:44:46.619Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/barelyhuman.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}},"created_at":"2020-12-01T18:54:22.000Z","updated_at":"2023-09-07T00:08:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"a3e5e635-078f-4114-81cb-a154c5481b11","html_url":"https://github.com/barelyhuman/background-timer","commit_stats":{"total_commits":30,"total_committers":2,"mean_commits":15.0,"dds":0.09999999999999998,"last_synced_commit":"44e2777a608e04a6b6d49674bfc1cc8f6d1329e8"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barelyhuman%2Fbackground-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barelyhuman%2Fbackground-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barelyhuman%2Fbackground-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barelyhuman%2Fbackground-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/barelyhuman","download_url":"https://codeload.github.com/barelyhuman/background-timer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248991532,"owners_count":21194893,"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":[],"created_at":"2024-10-01T08:44:49.217Z","updated_at":"2025-04-15T01:44:53.391Z","avatar_url":"https://github.com/barelyhuman.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# background-timer\n\nSimple react hook to run a timer in the background\n\n## Motivation\n\nNothing really, pulled it off a project I wrote it for and now it's a package.\n\n## Install\n\n```sh\nnpm i @barelyreaper/background-timer\n```\n\n## Usage\n\n### On Render Timer\n\nUsed when the timer is **not** dependant on some other property and can be run as soon as the page/component is rendered (example OTP validation screens)\n\n```js\nimport { useBackgroundTimer, msTimestamp } from 'background-timer';\n\nexport default function App() {\n  /**\n   * @param milliseconds\n   * @returns {timer: milliseconds, reset: function} resets the timer\n   */\n\n  const { timer, reset } = useBackgroundTimer(2 * 1000 * 60, {\n    onTimerEnd: () =\u003e {\n      alert('Timer Ended');\n    },\n  });\n\n  return (\n    \u003c\u003e\n      \u003cp\u003e{msTimestamp(timer)}\u003c/p\u003e\n      \u003cbutton disabled={timer \u003e 0} onClick={() =\u003e reset()}\u003e\n        Reset Timer\n      \u003c/button\u003e\n    \u003c/\u003e\n  );\n}\n```\n\n### Manually Control timer trigger\n\nUsed when the timer is dependant on some other property that might not be available during app render (example: Booking lock-in dependency)\n\n```js\nimport { msTimestamp, useManualTimer } from '@barelyreaper/background-timer';\nimport './App.css';\n\nfunction App() {\n  /**\n   * @param ticker delay provided to the internal interval, 1000 means timer executes every 1 second\n   * @param options={onTimerEnd?:function} optional options object that allows adding a callback on timer end\n   * @returns {timer: milliseconds, startTimer: function(millisecondsToExecFor){},stopTimer:function,stopped:Boolean}\n   */\n\n  const { startTimer, stopTimer, timer, stopped } = useManualTimer(1000, {\n    onTimerEnd: () =\u003e {\n      alert('Timer Ended');\n    },\n  });\n\n  return (\n    \u003cdiv className=\"App\"\u003e\n      \u003ch1 align=\"center\"\u003eManual Timer\u003c/h1\u003e\n      \u003cp\u003e{msTimestamp(timer)}\u003c/p\u003e\n      \u003cbutton onClick={() =\u003e startTimer(2 * 10 * 1000)}\u003e\n        {timer \u003e 0 ? 'Reset Timer' : 'Start Timer'}\n      \u003c/button\u003e\n      \u003cbutton\n        style={{ marginLeft: '8px' }}\n        disabled={timer \u003c 0 || stopped}\n        onClick={() =\u003e stopTimer()}\n      \u003e\n        Stop Timer\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n\nexport default App;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbarelyhuman%2Fbackground-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbarelyhuman%2Fbackground-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbarelyhuman%2Fbackground-timer/lists"}