gpt4 book ai didi

javascript - 当应用程序处于后台时,React Native setInterval 会暂停

转载 作者:行者123 更新时间:2023-11-29 16:01:46 26 4
gpt4 key购买 nike

我正在开发一个 react native 应用程序。对于其中一项任务,我需要使用 javascript setInterval 函数,但当应用程序处于后台时它会停止。谁能指导我,为什么它停止了?有什么解决办法吗?

我的代码:

 let startTimer = setInterval(() => {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
this.setState({ remainMin: minutes, remainSec: seconds });
if (--timer < 0) {
this.handelTimerStop();
timer = duration;
this.setState({ isResend: true });
}
}, 1000);

最佳答案

这是预期的行为 - 当应用程序暂停时,所有 setInterval 的运行和(以及 setTimeout 的挂起)也会暂停。您想查看后台任务,以在应用程序最小化时保持运行:

这应该可以帮助您实现:

How can I run background tasks in React Native?

关于javascript - 当应用程序处于后台时,React Native setInterval 会暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51997866/

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