Home > Software engineering >  Is there any way to show functions that are queued with setTimeOut() in react?
Is there any way to show functions that are queued with setTimeOut() in react?

Time:09-17

User login is managed with JWT stored in localStorage for my Website.
And within login function, setTimeout function exists and it clears the localStorage, when token expiration time has come.
But It doesn't work when the device has been to sleep mode. When it awakes, the time of clearing localStorage has already passed and logout function is not being called as I expected.
So if there is some way to run the function that are queued with 'setTimeout()', I would like to call it when device is awake from sleep mode.

Is it possible to call the functions that already has passed the time of setTimeout?

CodePudding user response:

No, but you can save the timeout to a variable and check if it's been cleared, and manually call function as needed:

const func = () => {
  console.log("hi")
}
const timeout = setTimeout(func, 1000)

// You'll have to implement a computer wake checker
onComputerWake(() => {
  if (!timeout) func()
})

But as another user mentioned in the comments, this is a terrible idea. JWTs have a built-in expiration. You should just have the server return 401, and redirect the user to the sign-in page if you receive 401 in your frontend.

  • Related