蕪湖不蕪
2023-05-18 10:08:46
我試圖每 5 分鐘發送一次動作。一旦用戶登錄,它就必須調度操作。它將在每 5 分鐘后開始調度操作。我嘗試使用setInterval,但這里的問題是即使我注銷它也會繼續執行調度操作。這是我的代碼,我在我的 app.js 中定義了這個 keepAlive 函數,我將整個應用程序包裝到 redux store 中。這是isAuthenticated布爾函數。如果isAuthenticated是 true 并且只有在我想派遣行動時才API.getAcessToken可用。localstorage function keepAlive() { if ( props.isAuthenticated === true && API.getAccessTokenFromLocalStorage() ) { setInterval(() => { props.keepTokenAlive(); }, 100000); // 1000ms =1sec } else { return null; } } keepAlive();
2 回答
四季花海
TA貢獻1811條經驗 獲得超5個贊
SMILET
TA貢獻1796條經驗 獲得超4個贊
你能做這個嗎?
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();
添加回答
舉報
0/150
提交
取消
