0

I’m using a Web Worker to set a setTimeout that automatically logs out the user after 10 minutes of inactivity. However, I’ve encountered an issue where if the user’s computer goes to sleep, all processes, including the Web Worker, stop running. This means that even if the session should have expired, the user remains logged in when the computer wakes up.

Is there a way to ensure that the user is logged out even if the computer goes to sleep and wakes up later? How can I handle this situation effectively?

Any suggestions or solutions would be greatly appreciated!

1
  • You should handle this on the server, not the client. Commented Aug 22, 2024 at 4:27

1 Answer 1

0

How about recording the time the user leaves the web and logs out if the time is more than 10 minutes?

let blurTime;

window.onblur = function() {
    blurTime = Date.now();
};

window.onfocus = function() {
    const focusTime = Date.now();
    const timeDifference = focusTime - blurTime;

    if (timeDifference > 10 * 60 * 1000) {
        logoutUser();
    }
};

Sign up to request clarification or add additional context in comments.

1 Comment

I think this should work! let me have a try, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.