Похожие чаты

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 ответов

10 просмотров

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

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

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

Какие дистрибутивы Linux чаще всего работают в качестве серверов и почему?
Alex
27
Такс, блин, таки кто-то знает, каким образом работают макросы stdin/stdout/stderr? Я влез в stdio.h, там определения нет, отладил через асмокод - вызывается функция со странны...
The Bird of Hermes
18
Ребят, привет! Я портируюсь с войда на генту Читаю гентувики В разделе про разбивку диска указана xfs файловая система, а не ext4, как обычно Xfs имеет какие-то преимущества ...
Андрей Дыбов
21
Всем привет, на линуксе лучше на fasm или nasm учиться писать для начала ?
meszjol
14
А кто-то пробовал, уезжая из Эстонии получить э-рез и продолжить вести предпринимательскую деятельность внутри Эстонии, используя свой OÜ?
Lalalashechki Lalala
62
I'm new at Monero and I'd like to ask you all - why are you in Monero? I know about privacy but what do you expect to happen in the next years, decades that Monero will make i...
Konrad
19
Господа, а как такое в миграцию засунуть?
Денис Александрович
12
Если у меня есть такой класс: Object = {} function Object:new(a_name, a_transform, a_color, a_mesh, a_material, a_shader, a_textures) local private = {} private.n...
Cuarno Vile
4
Is Kadena onto something special? No one really knows. Good luck!
Bright Uncle Stephen EqualBet
40
я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
Карта сайта