javascript - 如何在 reactjs 中每 5 分钟后调度一个动作
问题描述
我试图每隔 5 分钟发送一次动作。一旦用户登录,它就必须调度动作。它将每 5 分钟后开始调度动作。我尝试使用setInterval
,但这里的问题是即使我注销它也会继续调度操作。
这是我的代码我在我的 app.js 中定义了这个 keepAlive 函数,我将整个应用程序包装到 redux 商店中。这isAuthenticated
是布尔函数。如果isAuthenticated
为真并且仅API.getAcessToken
在其中可用,localstorage
那么我想调度操作。
function keepAlive() {
if (
props.isAuthenticated === true &&
API.getAccessTokenFromLocalStorage()
) {
setInterval(() => {
props.keepTokenAlive();
}, 100000); // 1000ms =1sec
} else {
return null;
}
}
keepAlive();
解决方案
你能做这个吗?
let to = null;
function keepAlive() {
//Assign a reference to clear the interval
to = setInterval(() => {
if (
props.isAuthenticated === true &&
API.getAccessTokenFromLocalStorage()
) {
props.keepTokenAlive();
} else {
// If not passing the condition, clear the interval
clearInterval(to);
}
}, 100000); // 1000ms =1sec
}
keepAlive();
推荐阅读
- javascript - 使用jquery在动态字段中更改事件
- python - 用于平方 (x^2) 近似的神经网络
- c# - 为 x64 和 x86 创建具有依赖项的安装程序
- reactjs - React - 在数组中显示项目的问题
- postgresql - 将 PostgreSQL 中的数据表复制到 CSV 时 COPY 0
- azure - 如何在网站上显示 Cosmos DB 数据
- ios - 从子类加载 Spritekit 场景
- c++ - SDL 窗口立即关闭
- python - 无法将字符串转换为浮点数:'4,49E+09
- protocol-buffers - proto 3 中的可选(重复)字段