当应用程序在后台打开超过30分钟时,Cordova会执行某些操作

问题描述 投票:1回答:1

我正在使用react和Cordova构建混合应用程序。在后台关闭应用程序30分钟后,我需要呈现特定的组件。

我有两个事件侦听器

  1. “ pause”,用​​于检测应用程序是否在后台打开。在这里,我将localStorage设置为当前日期/时间。

  2. “恢复”,即在后台打开应用程序后已打开它。在这里,我得到当前时间,即本地存储的时间,并检查两者之间的时间是否为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);
javascript reactjs cordova hybrid-mobile-app
1个回答
0
投票

[在为此感到头痛之后,我最终为此使用了反应闲置计时器。 https://www.npmjs.com/package/react-idle-timer

© www.soinside.com 2019 - 2024. All rights reserved.