Похожие чаты

I have a token which is set to expire after

60 days
I am setting the expiration time as


const now = new Date().getTime();
localStorage.setItem('Expiration', `${now + 1000 * 60 * 60 * 24 * 30}`);

Now I want to run a setTimeout for the remaining interval using useEffect
my useEffect looks as

useEffect(() => {
const expirationDuration = localStorage.getItem('Expiration');
let timeoutId: NodeJS.Timeout;

const currentTime = new Date().getTime();
if (!expirationDuration || currentTime > +expirationDuration) {
Cookies.remove('token');
localStorage.removeItem('userId');
localStorage.removeItem('Expiration');
router.replace('/signin');
} else {
const interval = +expirationDuration - currentTime;
const timer = () =>
setTimeout(() => {
Cookies.remove('token');
localStorage.removeItem('userId');
localStorage.removeItem('Expiration');
router.replace('/signin');
}, interval);

timeoutId = timer();
}

return () => {
clearTimeout(timeoutId);
};
}, []);
But apparently when I check in the debugger the setTimeout runs immediately after the page renders instead of running after the specified time interval. What am I doing wrong?

1 ответов

24 просмотра

Now I want to run a setTimeout for the remaining interval using useEffect my useEffect looks as

Похожие вопросы

Обсуждают сегодня

Карта сайта