Last active
June 22, 2024 14:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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