Gur*_*uru 8 javascript reactjs react-native
我正在开发一个反应本机应用程序。对于其中一项任务,我需要使用 javascriptsetInterval函数,但是当应用程序在后台时它会停止。谁能指导我,为什么它停止了?有什么解决办法吗?
我的代码:
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);
Run Code Online (Sandbox Code Playgroud)
setInterval这是预期的行为 - 当应用程序暂停时,所有的运行和(和setTimeout待处理)也会暂停。您想要查看后台任务,以便在应用程序最小化时保持某些内容运行:
这应该可以帮助您实现这一目标:
| 归档时间: |
|
| 查看次数: |
6385 次 |
| 最近记录: |