Skip to content

Instantly share code, notes, and snippets.

@cheskoxd
Last active June 22, 2024 14:14
Show Gist options
  • Save cheskoxd/12df5c45b0d893417c7b16ad63a78a82 to your computer and use it in GitHub Desktop.
Save cheskoxd/12df5c45b0d893417c7b16ad63a78a82 to your computer and use it in GitHub Desktop.
Small hook to check if the user has been inactive for x hours it will reload the page
import { useEffect, useRef } from 'react';
const useInactivityTimeout = (timeoutInHours) => {
const timeoutInMillis = timeoutInHours * 60 * 60 * 1000;
const timer = useRef(null);
useEffect(() => {
if (navigator.userActivation) {
if(navigator.userActivation.isActive){
if (timer.current) {
clearTimeout(timer.current);
}
} else {
timer.current = setTimeout(() => {
window.location.reload();
}, timeoutInMillis);
}
}
return () => {
if (timer.current) {
clearTimeout(timer.current);
}
};
}, [timeoutInMillis]);
};
export default useInactivityTimeout;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment