@boterop/react-native-background-timer

中文文档
2.6.0发布于 2025/04/10MITTypeScriptCommonJS零依赖近 30 天下载 3489

Emit event periodically (even when app is in the background)

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Crossplatform
  4. iOS
  5. Android
  6. Obsolete

React Native Background Timer

Emit event periodically (even when app is in the background).

Installation

  1. If you use Expo to create a project you'll just need to "eject".

    expo eject
    
  2. Install React Native Background Timer package.

    yarn add @boterop/react-native-background-timer
    # or using npm
    npm install @boterop/react-native-background-timer --save
    
  3. Link React Native Background Timer library. This step is not necessary when you use React Native >= 0.60 (and your app is not ejected from Expo).

    react-native link @boterop/react-native-background-timer
    
  4. If you use CocoaPods or React Native >= 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:

    cd ios
    pod install
    

Link the library manually if you get errors:

  • Android: TypeError: Cannot read property 'setTimeout' of undefined or TypeError: null is not an object (evaluating 'RNBackgroundTimer.setTimeout')
  • iOS: Native module cannot be null
Android manual linking
  • android/settings.gradle

    + include ':react-native-background-timer'
    + project(':react-native-background-timer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-timer/android')
    
  • android/app/build.gradle

    dependencies {
    +   implementation project(':react-native-background-timer')
    }
    
  • android/app/src/main/java/com/your-app/MainApplication.java

    ```diff
    + import io.boterop.timer.BackgroundTimerPackage;
    
    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
    +   new BackgroundTimerPackage()
      );
    }
    ```
    
iOS manual linking
  • ios/Podfile

    ```diff
    + pod '@boterop/react-native-background-timer', :path => '../node_modules/@boterop/react-native-background-timer'
    ```
    

Usage

import BackgroundTimer from '@boterop/react-native-background-timer';

Crossplatform

To 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.

BackgroundTimer.runBackgroundTimer(() => {
    //code that will be called every 3 seconds
}, 3000);
//rest of code will be performing for iOS on background too

BackgroundTimer.stopBackgroundTimer(); //after this call all code on background stop run.

iOS

After iOS update logic of background task little bit changed. So we can't use as it was. You have to use only start() and stop() without parameters. And all code that is performing will continue performing on background including all setTimeout() timers.

Example:

BackgroundTimer.start();
// Do whatever you want incuding setTimeout;
BackgroundTimer.stop();

If you call stop() on background no new tasks will be started! Don't call .start() twice, as it stop performing previous background task and starts new. If it will be called on backgound no tasks will run.

Android

You can use the setInterval and setTimeout functions. This API is identical to that of react-native and can be used to quickly replace existing timers with background timers.

// Start a timer that runs continuous after X milliseconds
const intervalId = BackgroundTimer.setInterval(() => {
    // this will be executed every 200 ms
    // even when app is the the background
    console.log('tic');
}, 200);

// Cancel the timer when you are done with it
BackgroundTimer.clearInterval(intervalId);
// Start a timer that runs once after X milliseconds
const timeoutId = BackgroundTimer.setTimeout(() => {
    // this will be executed once after 10 seconds
    // even when app is the the background
    console.log('tac');
}, 10000);

// Cancel the timeout if necessary
BackgroundTimer.clearTimeout(timeoutId);

Obsolete

Obsolete usage which doesn't support multiple background timers.

import {
    DeviceEventEmitter,
    NativeAppEventEmitter,
    Platform,
} from 'react-native';

import BackgroundTimer from '@boterop/react-native-background-timer';
const EventEmitter = Platform.select({
    ios: () => NativeAppEventEmitter,
    android: () => DeviceEventEmitter,
})();
// start a global timer
BackgroundTimer.start(5000); // delay in milliseconds only for Android
// listen for event
EventEmitter.addListener('backgroundTimer', () => {
    // this will be executed once after 5 seconds
    console.log('toe');
});
// stop the timer
BackgroundTimer.stop();

版本与发布记录

@boterop/react-native-background-timer 共发布 4 个版本,首次发布于 2024/04/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.6.0latest2025/04/101.6 年前
2.5.22024/08/162 年前
2.5.12024/04/282 年前
2.5.02024/04/282 年前

关于 @boterop/react-native-background-timer 的常见问题

@boterop/react-native-background-timer 是什么?

Emit event periodically (even when app is in the background)

如何安装 @boterop/react-native-background-timer?

在项目目录运行 npm install @boterop/react-native-background-timer,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react-native(>=0.47.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@boterop/react-native-background-timer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@boterop/react-native-background-timer 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@boterop/react-native-background-timer 可以商用吗?

@boterop/react-native-background-timer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@boterop/react-native-background-timer 的下载量有多少?

根据 npm 官方下载统计,@boterop/react-native-background-timer 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,489 次,日均约 116 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@boterop/react-native-background-timer 还在维护吗?

最新版本 2.6.0 发布于 2025/04/10(1.6 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/04/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。