Javascript – Check if user is idle

Update @ 2016-05-21: Please consider using the idleCat plugin instead of this brute force checking approach. Thanks for the suggestion from Smuuf. =D

 

Nowadays, websites are becoming more advanced with many different features. Sometimes it would be beneficial if we could switch off them on the client browser when the user is idle for a period of time in order to reduce the web server loading.

Here is a Javascript example which would pop up an alert box if the mouse doesn’t move for 3 mins.

$(document).ready(function () {
  // Set timer to check if user is idle
  var idleTimer;
  $(this).mousemove(function(e){
    // clear prior timeout, if any
    window.clearTimeout(idleTimer);

    // create new timeout (3 mins)
    idleTimer = window.setTimeout(isIdle, 180000);
  });

  function isIdle() {
    alert("3 mins have passed without moving the mouse.");
  }
});

 

Done =)

Reference: StackOverflow – Detecting idle time in JavaScript elegantly

Advertisements

3 thoughts on “Javascript – Check if user is idle”

  1. Whoa, doing this for EVERY mouse move event generated by the browser sounds like a really expensive thing to do. I would argue that doing this only when N have seconds passed is a more appropriate way to do.

    As it is done in the jQuery plugin idleCat: https://github.com/smuuf/jquery-idlecat … Disclaimer: I’m the author of the plugin.

    Like

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