在后台( 即使在应用程序处于活动状态时) 定期发出Emit事件

・4 分钟阅读

  • 源代码名称: react-native-background-timer
  • 源代码网址: https://www.github.com/ocetnik/react-native-background-timer
  • react-native-background-timer的文档
  • react-native-background-timer的源代码下载
  • Git URL:
    git://www.github.com/ocetnik/react-native-background-timer.git
  • Git Clone代码到本地:
    git clone https://www.github.com/ocetnik/react-native-background-timer
  • Subversion代码到本地:
    $ svn co --depth empty https://www.github.com/ocetnik/react-native-background-timer
                              Checked out revision 1.
                              $ cd repo
                              $ svn up trunk
              
  • React Native后台计时器

    定期发出事件(即使应用在后台)。

    安装

    • npm i react-native-background-timer --save
    • react-native link

    在iOS上使用CocoaPods

    • npm i react-native-background-timer --save
    • 将以下内容添加到你的pod文件中: pod 'react-native-background-timer', :path => '../node_modules/react-native-background-timer'

    使用Crossplatform

    在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 tooBackgroundTimer.stopBackgroundTimer(); //after this call all code on background stop run.

    Android还没有测试。

    使用iOS

    iOS后台任务的更新逻辑更改后,所以我们不能用它,你只能使用不带参数的start()和stop(),所有正在执行的代码都将在后台执行,包括所有setTimeout()定时器。

    例如:

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

    如果在后台调用stop(),则不会启动新任务!不要调用.start()两次,因为它停止执行以前的后台任务,并且开始新的任务,如果在后台调用,则不会运行任务。

    使用Android

    你可以使用setIntervalsetTimeout函数,此API与react-native相同,可以使用后台计时器快速替换现有计时器。

    importBackgroundTimerfrom'react-native-background-timer';
    // Start a timer that runs continuous after X millisecondsconstintervalId=BackgroundTimer.setInterval(() => {
     // this will be executed every 200 ms// even when app is the the backgroundconsole.log('tic');
    }, 200);// Cancel the timer when you are done with itBackgroundTimer.clearInterval(intervalId);
    // Start a timer that runs once after X millisecondsconsttimeoutId=BackgroundTimer.setTimeout(() => {
     // this will be executed once after 10 seconds// even when app is the the backgroundconsole.log('tac');
    }, 10000);// Cancel the timeout if necessaryBackgroundTimer.clearTimeout(timeoutId);

    已过时

    过时的用法,不允许使用多个后台计时器。

    import {
     DeviceEventEmitter,
     NativeAppEventEmitter,
     Platform,
    } from'react-native';importBackgroundTimerfrom'react-native-background-timer';
    constEventEmitter=Platform.select({
     ios: () => NativeAppEventEmitter,
     android: () => DeviceEventEmitter,
    })();
    // start a global timerBackgroundTimer.start(5000); // delay in milliseconds only for Android
    // listen for eventEventEmitter.addListener('backgroundTimer', () => {
     // this will be executed once after 5 secondsconsole.log('toe');
    });
    // stop the timerBackgroundTimer.stop();
    讨论
    Fansisi profile image