gpt4 book ai didi

javascript - 在 React Native Android 和 iOS 中使用 Set Interval

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:23:51 24 4
gpt4 key购买 nike

两者都是React Native Video和计时器(使用 setInterval)在前台工作,当应用程序在后台时计时器停止。但视频仍在后台播放。

这意味着,当应用程序暂停时,所有 setInterval 的运行和(以及 setTimeouts 挂起)也会暂停。

headlessJs api 仅适用于 android 以在后台运行任务。

If I use headlessJs, Isn't product pain for react native to run a timer?


有一些想要的库,

react-native-background-job - 使用 headlessJs 并仅在 android 中工作。

react-native-background-task - 在android 和ios 中使用headlessJs 围绕react-native-background-fetch 库使用代理(仅支持单个任务,任务执行的确切时间不可预测)

react-native-background-fetch

Can I use setInterval in the background service if I use those libraries?

Why only music player is working in the background?

最佳答案

我从 react-native-background-timer 找到了解决方案图书馆。

在 iOS 中,我们可以在执行以下代码后启动计时器或在后台执行任何 React Native 操作。

即使我们在设备中使用其他应用程序,后台计时器也能正常工作。 (例如:itune,whatsup)

// Import the library
import BackgroundTimer from 'react-native-background-timer';

// Start the timer
BackgroundTimer.start();

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

在安卓中,

// Import the library
import BackgroundTimer from 'react-native-background-timer';

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

注意:用设备测试,不要相信模拟器

关于javascript - 在 React Native Android 和 iOS 中使用 Set Interval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55039857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com