# @boterop/react-native-background-timer 中文文档

规范地址：https://npmcn.com/package/%40boterop/react-native-background-timer/

版本：2\.6\.0
npm 元数据更新时间：2025-04-09T21:25:12.813Z
中文译文更新时间：2026-11-08T12:13:11.758Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40boterop/react-native-background-timer
原文对照：https://npmcn.com/package/%40boterop/react-native-background-timer/?lang=original
项目主页：https://github.com/boterop/react-native-background-timer#readme
源码仓库：https://github.com/boterop/react-native-background-timer

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：34.6 kB，22 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：react-native \>=0\.47\.0
- 首次发布：2024-04-27T21:40:32.133Z
- 最新版本发布：2025-04-09T21:25:12.613Z
- 近 30 天下载量：3489（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 0 个）

## 中文简介

定期发送事件( 即使应用在背景中)

## 安装

```sh
npm install @boterop/react-native-background-timer
```

也可以使用：`pnpm add @boterop/react-native-background-timer`、`yarn add @boterop/react-native-background-timer`、`bun add @boterop/react-native-background-timer`

## README

# 反应原生背景计时器

定期发布事件(即使应用在背景中).

## 安装

1.  如果您使用 Expo 创建工程 [你会只需要](https://facebook.github.io/react-native/docs/getting-started#caveats) " (中文(简体) )[弹出](https://docs.expo.io/versions/latest/expokit/eject)" ..
    
    ```bash
    expo eject
    ```
    
2.  安装本地背景计时器包 .
    
    ```bash
    yarn add @boterop/react-native-background-timer
    # or using npm
    npm install @boterop/react-native-background-timer --save
    ```
    
3.  Link React 原生背景计时器库. 当您使用 React Industrial QQ 0.60(且您的应用程序没有被从 Expo 中弹出)时,此步骤没有必要.
    
    ```bash
    react-native link @boterop/react-native-background-timer
    ```
    
4.  如果您使用 CocoaPods 或 React Introduct 0.60 (且您的应用程序没有被从 Expo 中弹出) 或您的应用程序被从 Expo 中弹出, 那么在运行您的应用程序在 iOS 上之前, 请确保您安装并运行 CocoaPods :
    
    ```bash
    cd ios
    pod install
    ```
    

错误时手动链接库 :

-   机器人: `TypeError: Cannot read property 'setTimeout' of undefined` 或者说 `TypeError: null is not an object (evaluating 'RNBackgroundTimer.setTimeout')`
-   iOS: (英语) `Native module cannot be null`

Android 手册链接

-   `android/settings.gradle`
    
    ```diff
    + 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`
    
    ```diff
    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 手册链接

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

## 使用量

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

### 跨平台

要在Android和iOS上使用相同的代码,则使用 runBackgroundTimer ()和 stopBackgroundTimer (). 只能使用一个背景计时器来保持代码的一致性.

```js
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系统

在iOS更新背景任务的逻辑稍有改变后. 所以我们不能像以前那样使用. 您只需使用没有参数的起始( ) 和停止( ) 。 而所有正在执行的代码将继续在背景上执行,包括所有设置的Timeout()定时器.

示例

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

> 如果您在背景上呼叫停止( ) , 将不会启动新任务 ! 不要两次拨打.start(),因为它停止执行之前的背景任务并开始新的任务. 如果用后台调用的话, 任务就不会完成 .

### 机器人

你可以用这个 `setInterval` 财务报告和审定财务报表 `setTimeout` 函数. 此API与 `react-native` 并可用于快速替换已存在的定时器 带有背景计时器.

```js
// 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);
```

```js
// 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);
```

### 过时

过时的使用不支持多个背景计时器.

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

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

```js
const EventEmitter = Platform.select({
    ios: () => NativeAppEventEmitter,
    android: () => DeviceEventEmitter,
})();
```

```js
// start a global timer
BackgroundTimer.start(5000); // delay in milliseconds only for Android
```

```js
// listen for event
EventEmitter.addListener('backgroundTimer', () => {
    // this will be executed once after 5 seconds
    console.log('toe');
});
```

```js
// stop the timer
BackgroundTimer.stop();
```

## 常见问题

### @boterop/react-native-background-timer 是什么？

定期发送事件\( 即使应用在背景中\)

### 如何安装 @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 和提交记录判断。

