Skip to content

Instantly share code, notes, and snippets.

@gerard-kanters
Last active April 29, 2025 17:25
Show Gist options
  • Select an option

  • Save gerard-kanters/2ce9daa5c23d8abe36c2 to your computer and use it in GitHub Desktop.

Select an option

Save gerard-kanters/2ce9daa5c23d8abe36c2 to your computer and use it in GitHub Desktop.
Inactivity timeout javascript
<script type="text/javascript">
function idleTimer() {
var t;
//window.onload = resetTimer;
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer; // catches mouse clicks
window.onscroll = resetTimer; // catches scrolling
window.onkeypress = resetTimer; //catches keyboard actions
function logout() {
window.location.href = '/action/logout'; //Adapt to actual logout script
}
function reload() {
window.location = self.location.href; //Reloads the current page
}
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 1800000); // time is in milliseconds (1000 is 1 second)
t= setTimeout(reload, 300000); // time is in milliseconds (1000 is 1 second)
}
}
idleTimer();
</script>
@alanensina

Copy link
Copy Markdown

Thanks @gerard-kanters, this helped me a lot. :)

@kalyondo

Copy link
Copy Markdown

WOw! After tens of google searches, I seem to have made head ways, @gerard-kanters thanks many.

@culniga

culniga commented Jul 24, 2019

Copy link
Copy Markdown

Instead of listing number of events you can use events on window.blur to clear timer and window.focus to start timer
You can modify this solution: https://stackoverflow.com/questions/1760250/how-to-tell-if-browser-tab-is-active

@ndraiman

Copy link
Copy Markdown

@culniga You can use the browser's Page Visibility API instead, its more reliable.

@starbuck93

Copy link
Copy Markdown

@nexxado, good suggestion:

document.addEventListener('visibilitychange', function () {
    var t;
    if (document.hidden) {
        // start inactivity timeout
        
        function logout(){
            window.location.href = '/logout';
        }
        t = setTimeout(logout, 1800000);
    } else {
        clearTimeout(t);
    }
});

@Robin-the-Frog

Robin-the-Frog commented Oct 19, 2020

Copy link
Copy Markdown

@gerard-kanters
Like some others I found this after hours of fruitless Google searching. Thanks so much, this is working well for me. I have commented out the reload sections though.
How could I alter the code (I'm pretty much a noob in js) so that the code ignores my 'login' page?
Thanks

@sankhadipsen

Copy link
Copy Markdown

There's one in my Repo in case someone wants to check.

@EynoRahul

Copy link
Copy Markdown

if the admin wants that on that page he pauses for 15 mins data will be automatically saved into the database then how to identify the page.

@davidhyland

davidhyland commented Oct 30, 2024

Copy link
Copy Markdown

I realise this is pretty old but this is awesome, thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment