我想在我的應用程序中創建一個計時器。我創建了一個定時器爲:React Native,在活動暫停時停止setInterval
var timer = document.getElementById('timer');
var rest = 90;
timer.innerHTML = rest + 's';
var interval = setInterval(function(){
if(rest <= 0){
clearInterval(interval);
}else{
rest -= 1;
timer.innerHTML = rest + 's';
}
}, 1000);
<div id='timer'></div>
但是,當用戶離開我的應用程序(不殺,見好就收),去到任何其他應用程序,計時器停止工作。
當他回到我的應用程序,計時器開始工作。
對於我來說,當你的應用程序被殺死時,可以使用後臺任務。在我的情況下,我只是想當用戶鎖定他的手機時不停止定時器。 –
我認爲讓你的'後臺任務'成爲可能的唯一選擇是完全實現本地化。但是,我沒有經驗給你正確的答案。我剛剛找到2個鏈接,我將分享給你,也許他們幫助你: https://facebook.github.io/react-native/docs/native-modules-android.html https:// developer。 android.com/training/run-background-service/create-service.html –