我正在开发一个反应本机应用程序。对于其中一项任务,我需要使用 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)