{"id":13777163,"url":"https://github.com/ocetnik/react-native-background-timer","last_synced_at":"2025-05-14T09:07:19.250Z","repository":{"id":37444628,"uuid":"58488899","full_name":"ocetnik/react-native-background-timer","owner":"ocetnik","description":"Emit event periodically (even when app is in the background)","archived":false,"fork":false,"pushed_at":"2023-10-25T15:45:46.000Z","size":2051,"stargazers_count":1645,"open_issues_count":125,"forks_count":235,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-05-12T06:28:23.672Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","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/ocetnik.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}},"created_at":"2016-05-10T19:42:56.000Z","updated_at":"2025-05-04T12:15:30.000Z","dependencies_parsed_at":"2023-02-08T04:45:15.040Z","dependency_job_id":"196a607a-1612-4fea-b0a1-50fbf11fd1c9","html_url":"https://github.com/ocetnik/react-native-background-timer","commit_stats":{"total_commits":350,"total_committers":31,"mean_commits":"11.290322580645162","dds":0.5914285714285714,"last_synced_commit":"ad04db98c83ea0f0aa3c1382eda55b4dbe4497f3"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocetnik%2Freact-native-background-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocetnik%2Freact-native-background-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocetnik%2Freact-native-background-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocetnik%2Freact-native-background-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ocetnik","download_url":"https://codeload.github.com/ocetnik/react-native-background-timer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254110374,"owners_count":22016391,"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-08-03T18:00:38.611Z","updated_at":"2025-05-14T09:07:19.205Z","avatar_url":"https://github.com/ocetnik.png","language":"Java","readme":"# React Native Background Timer\nEmit event periodically (even when app is in the background).\n\n## Installation\n1. If you use Expo to create a project [you'll just need to](https://facebook.github.io/react-native/docs/getting-started#caveats) \"[eject](https://docs.expo.io/versions/latest/expokit/eject)\".\n\n    ```bash\n    expo eject\n    ```\n\n2. Install React Native Background Timer package.\n\n    ```bash\n    yarn add react-native-background-timer\n    # or using npm\n    npm install react-native-background-timer --save\n    ```\n\n3. Link React Native Background Timer library. This step is not necessary when you use React Native \u003e= 0.60 (and your app is not ejected from Expo).\n\n    ```bash\n    react-native link react-native-background-timer\n    ```\n\n4. If you use CocoaPods or React Native \u003e= 0.60 (and your app is not ejected from Expo) or your app is ejected from Expo, then before running your app on iOS, make sure you have CocoaPods installed and run:\n\n    ```bash\n    cd ios\n    pod install\n    ```\n\nLink the library manually if you get errors:\n\n- Android: `TypeError: Cannot read property 'setTimeout' of undefined` or `TypeError: null is not an object (evaluating 'RNBackgroundTimer.setTimeout')`\n- iOS: `Native module cannot be null`\n\n\u003cdetails\u003e\n    \u003csummary\u003eAndroid manual linking\u003c/summary\u003e\n\n- `android/settings.gradle`\n\n    ```diff\n    + include ':react-native-background-timer'\n    + project(':react-native-background-timer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-timer/android')\n    ```\n\n- `android/app/build.gradle`\n\n    ```diff\n    dependencies {\n    +   implementation project(':react-native-background-timer')\n    }\n    ```\n\n- `android/app/src/main/java/com/your-app/MainApplication.java`\n\n    ```diff\n    + import com.ocetnik.timer.BackgroundTimerPackage;\n\n    @Override\n    protected List\u003cReactPackage\u003e getPackages() {\n      return Arrays.\u003cReactPackage\u003easList(\n    +   new BackgroundTimerPackage()\n      );\n    }\n    ```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eiOS manual linking\u003c/summary\u003e\n\n- `ios/Podfile`\n\n    ```diff\n    + pod 'react-native-background-timer', :path =\u003e '../node_modules/react-native-background-timer'\n    ```\n\u003c/details\u003e\n\n## Usage\n\n```js\nimport BackgroundTimer from 'react-native-background-timer';\n```\n\n### Crossplatform\nTo use the same code both on Android and iOS use runBackgroundTimer() and stopBackgroundTimer(). There can be used only one background timer to keep code consistent.\n\n```js\nBackgroundTimer.runBackgroundTimer(() =\u003e { \n//code that will be called every 3 seconds \n}, \n3000);\n//rest of code will be performing for iOS on background too\n\nBackgroundTimer.stopBackgroundTimer(); //after this call all code on background stop run.\n```\n\n### iOS\nAfter iOS update logic of background task little bit changed. So we can't use as it was. \nYou have to use only start() and stop() without parameters. And all code that is performing will continue performing on background including all setTimeout() timers.\n\nExample:\n```js\nBackgroundTimer.start();\n// Do whatever you want incuding setTimeout;\nBackgroundTimer.stop();\n```\n\n\u003e If you call stop() on background no new tasks will be started!\n\u003e Don't call .start() twice, as it stop performing previous background task and starts new. \n\u003e If it will be called on backgound no tasks will run.\n\n### Android\nYou can use the `setInterval` and `setTimeout` functions.\nThis API is identical to that of `react-native` and can be used to quickly replace existing timers\nwith background timers.\n\n```js\n// Start a timer that runs continuous after X milliseconds\nconst intervalId = BackgroundTimer.setInterval(() =\u003e {\n\t// this will be executed every 200 ms\n\t// even when app is the the background\n\tconsole.log('tic');\n}, 200);\n\n// Cancel the timer when you are done with it\nBackgroundTimer.clearInterval(intervalId);\n```\n\n```js\n// Start a timer that runs once after X milliseconds\nconst timeoutId = BackgroundTimer.setTimeout(() =\u003e {\n\t// this will be executed once after 10 seconds\n\t// even when app is the the background\n  \tconsole.log('tac');\n}, 10000);\n\n// Cancel the timeout if necessary\nBackgroundTimer.clearTimeout(timeoutId);\n```\n\n### Obsolete\nObsolete usage which doesn't support multiple background timers.\n\n```js\nimport {\n  DeviceEventEmitter,\n  NativeAppEventEmitter,\n  Platform,\n} from 'react-native';\n\nimport BackgroundTimer from 'react-native-background-timer';\n```\n\n```js\nconst EventEmitter = Platform.select({\n  ios: () =\u003e NativeAppEventEmitter,\n  android: () =\u003e DeviceEventEmitter,\n})();\n```\n\n```js\n// start a global timer\nBackgroundTimer.start(5000); // delay in milliseconds only for Android\n```\n```js\n// listen for event\nEventEmitter.addListener('backgroundTimer', () =\u003e {\n\t// this will be executed once after 5 seconds\n\tconsole.log('toe');\n});\n```\n```js\n// stop the timer\nBackgroundTimer.stop();\n```\n","funding_links":[],"categories":["\u003ca name=\"OS-\u0026-System-\u0026-File-Manager:-Native-Modules\"\u003eOS, System \u0026 File Manager: Native Modules\u003c/a\u003e","Java"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focetnik%2Freact-native-background-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Focetnik%2Freact-native-background-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focetnik%2Freact-native-background-timer/lists"}