Skip to content

Detect user idle in pure JavaScript

10 November 2016
function detectIdle() {
    var timer,
    timeout = 10; // timeout in seconds

    function idle() {
        console.log('You have been idle for', timeout, 'seconds');
        // resetTimer(); // repeated detection
    }

    function resetTimer() {
        clearTimeout(timer);
        timer = setTimeout(idle, timeout * 1000);
    }

    document.onmousemove = resetTimer;
    document.onkeypress = resetTimer;
    resetTimer(); // or window.onload = resetTimer;
}
Advertisements

From → Howto, JavaScript

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: