我正在使用react和Cordova构建混合应用程序。在后台关闭应用程序30分钟后,我需要呈现特定的组件。
我有两个事件侦听器
“ pause”,用于检测应用程序是否在后台打开。在这里,我将localStorage设置为当前日期/时间。
“恢复”,即在后台打开应用程序后已打开它。在这里,我得到当前时间,即本地存储的时间,并检查两者之间的时间是否为30分钟。这是我的问题所在,它总是返回false。我的问题是更多与javascript相关的反应/ cordova。有人可以解释我在做什么错吗?
// when the app is open in the backgroud
document.addEventListener("pause", () => {
localStorage.setItem("appTimeout", Date.now());
},false);
// when you reopen the app
document.addEventListener("resume", () => {
setTimeout(() => {
const minutes = 1000 * 30 * 60;
const closeTime = localStorage.getItem("appTimeout");
if(Date.now() - Number(closeTime) >= minutes){
//render component
}
}, 0);
},false);
[在为此感到头痛之后,我最终为此使用了反应闲置计时器。 https://www.npmjs.com/package/react-idle-timer