Daily Code: Day 3 – Messing with Idle Users

Daily Code: Day 3 – Messing with Idle Users

Daily Code: Day 3 – Messing with Idle Users

jQuery binding and idle users on Day 3 of Daily Code!

Having fun in timeout

Sometimes users disappear for a bit. Maybe you would like to welcome back with a greeting or a special animation. Or in this case, you just want to mess with them.

Well, we need to figure out when the user goes idle as well as when the user returns for interaction.

First, the variables:

These variables get set outside of all functions so they can be accessed globally.

idleTimer is our place holder for the timer function. It is initially set to null so we can define the timer when needed.

idleState is our reference to whether the user is idle or not. Iitially set to false so the user does not start out idle.

idleStatePrev is our reference to the previous idle state. Used to determine non-idle state.

idleWait is our idle timeout duration in milliseconds. 10000 = 10 seconds.

Next, the binding:

$('*') targets all elements.

.on(...) binds the elements to triggers.

'mousemove keydown scroll' defines the specific triggers we are looking for.

function(){ ... } is called when a trigger is fired from a bound element.

Finally, the timer:

All of the following code goes inside the binding function declared above.

First, we need to clear out our idle timer variable using clearTimeout(idleTimer);

Next, if (!idleState && !idleStatePrev) { checks to see if both the idleState and idleStatePrev are false which shows that the user has left the idle state.

If both arguments return false, it will preform the following:

$("#timer span").html("You are not idle"); prints out indicator text in the navigation of this page.

alert("Welcome Back!\nYou're awesome!\nI hope you like your surprise...") welcomes the user back with a nice message.

$('*').css('background-color', '#' + Math.floor(Math.random()*16777215).toString(16) ) sets the background-color of all elements to a random hex color (just for fun).

And, we set idleStatePrev = true to reset the idle status.

idleState = false basically resets the idle status.

Then, we define our idleTimer variable with a setTimeout function.

$("#timer span").html("You are idle"); prints out indicator text in the navigation of this page.
idleState = true lets us know that the user is now idle.
idleStatePrev = false helps us determine if the user is still idle (see if condition above).
...}, idleWait) tells the setTimeout function how long to wait as defined in our global variables.

Summing up:

Lastly, we need to fire an initial trigger of mousemove on the body to initiate the idle monitoring.

Here is the entire code.

Weird Chrome Bug Note:
There is a weird bug in Google Chrome that will fire mousemove every 1 – 5 seconds ONLY IF you have some specific things running on your machine. Particularly, programs that monitor on a frequent basis (Task Manager, Resource Managers) or music players can shift focus (iTunes).

Join the Conversation

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen − 6 =