@boterop/react-native-background-timer

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

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

中文使用文档
文档目录6 节
  1. 安装
  2. 使用量
  3. 跨平台
  4. iOS系统
  5. 机器人
  6. 过时

反应原生背景计时器

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

安装

  1. 如果您使用 Expo 创建工程 你会只需要 " (中文(简体) )弹出" ..

    expo eject
    
  2. 安装本地背景计时器包 .

    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 中弹出)时,此步骤没有必要.

    react-native link @boterop/react-native-background-timer
    
  4. 如果您使用 CocoaPods 或 React Introduct 0.60 (且您的应用程序没有被从 Expo 中弹出) 或您的应用程序被从 Expo 中弹出, 那么在运行您的应用程序在 iOS 上之前, 请确保您安装并运行 CocoaPods :

    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

    + 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 手册链接
  • ios/Podfile

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

使用量

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

跨平台

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

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()定时器.

示例

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

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

机器人

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

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

过时

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

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 是什么?

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

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