views:

1348

answers:

7

I am needing to detect when a user is inactive (not clicking or typing) on the current page for more than 30 minutes.

I thinking it might be best to use event blubbling attached to the body tag and then just keep resetting a timer for 30 minutes, but I'm not exactly sure how to create this.

I have jQuery available, although I'm not sure how much of this will actually use jQuery.

Edit: I'm more needing to know if they are actively using the site, therefore clicking (changing fields or position within a field or selecting checkboxes/radios) or typing (in an input, textarea, etc). If they are in another tab or using another program, then my assumption is they are not using the site and therefore should be logged out (for security reasons).

Edit #2: So everyone is clear, this is not at all for determining if the user is logged in, authenticated or anything. Right now the server will log the user out if they don't make a page request within 30 minutes. This functionality to prevent the times when someone spends >30 minutes filling in a form and then submitting the form only to find out that they haven't been logged out. Therefore, this will be used in combination with the server site to determine if the user is inactive (not clicking or typing). Basically, the deal is that after 25 minutes of idle, they will be presented with a dialog to enter their password. If they don't within 5 minutes, the system automatically logs them out as well as the server's session is logged out (next time a page is accessed, as with most sites).

The Javascript is only used as a warning to user. If JavaScript is disabled, then they won't get the warning and (along with most of the site not working) they will be logged out next time they request a new page.

+4  A: 

You can watch mouse movement, but that's about the best you're going to get for indication of a user still being there without listening to the click event. But there is no way for javascript to tell if it is the active tab or if the browser is even open. (well, you could get the width and height of the browser and that'd tell you if it was minimized)

Malfist
also you can look at the keyup event
TStamper
The OP said not clicking and not typing, so there'd be no keyup event.
Malfist
what do you mean there would be no keyup event? When the user stops typing, that is the last keyup event and by having a keyup function event cant you just check to see how long ago that keyup event occured
TStamper
Right, but the user isn't going to hold the key down for 30 minutes. There would be a key up event after the last key is typed, but the user has to type to get a key up event.
Malfist
Malfist, TStamper has it right. I can detect when the last keystroke or click was and calculate from there.
Darryl Hein
You can, but a mouse movement is more reliable. The user is almost certain to move the mouse at some point while the page is active. And even then, if you want, you can check for both events. or every event there is that javascript will allow you to check for. :)
Malfist
@Malfist-exactly what I meant, to check for both, not just one. that is why I said 'also look at the keyup event'
TStamper
@TStamper: agreed. Imagine spending a half hour typing up a long letter into the "complaints" box, only to have the site log you out just as you were putting the final flourish on a paragraph-long closing insult. It'd be infuriating...
Shog9
+1  A: 

Using jQuery, you can easily watch mouse movement, and use it to set a variable indicating activity to true, then using vanilla javascript, you can check this variable every 30 minutes (or any other interval) to see if its true. If it's false, run your function or whatever. Look up setTimeout and setInterval for doing the timing. You'll also probably have to run a function every minute or so to reset the variable to false.

Shadow
you're about 4 minutes late.
Malfist
+1  A: 

Here my shot:

var lastActivityDateTime = null;

function checkActivity( )
{
    var currentTime = new Date();
    var diff = (lastActivityDateTime.getTime( ) - currentTime.getTime( ));
    if ( diff >= 30*60*1000)
    {
        //user wasn't active;
        ...
    }
    setTimeout( 30*60*1000-diff, checkActivity);
}

setTimeout( 30*60*1000, checkActivity); // for first time we setup for 30 min.


// for each event define handler and inside update global timer
$( "body").live( "event_you_want_to_track", handler);

function handler()
{
   lastActivityDateTime = new Date();
   // rest of your code if needed.
}
Artem Barger
2 questions: (1) is there a reason why you wouldn't just keep reseting the timer and (2) why use live? The body tag is never removed or added, or am I missing something?
Darryl Hein
1. I'm reseting the timer, or I didn't understand your question. 2. Used live, just because 5 sec. before used in mine code.
Artem Barger
@Artem- live() is for looking for new dynamically created elements
TStamper
How are you resetting the timer? The setTimeout is just creating another timeout everytime the event happens.
Darryl Hein
@Darryl: checkActivity is just running periodically. the actual work of checking whether 30 minutes has passsed is done by comparing currentTime with lastActivityDateTime
Breton
+2  A: 

I just recently did something like this, albeit using Prototype instead of JQuery, but I imagine the implementation would be roughly the same as long as JQuery supports custom events.

In a nutshell, IdleMonitor is a class that observes mouse and keyboard events (adjust accordingly for your needs). Every 30 seconds it resets the timer and broadcasts an state:idle event, unless it gets a mouse/key event, in which case it broadcasts a state:active event.

var IdleMonitor = Class.create({

    debug: false,
    idleInterval: 30000, // idle interval, in milliseconds
    active: null,
    initialize: function() {
        document.observe("mousemove", this.sendActiveSignal.bind(this));
        document.observe("keypress", this.sendActiveSignal.bind(this));
        this.timer = setTimeout(this.sendIdleSignal.bind(this), this.idleInterval);
    },

    // use this to override the default idleInterval
    useInterval: function(ii) {
     this.idleInterval = ii;
     clearTimeout(this.timer);
     this.timer = setTimeout(this.sendIdleSignal.bind(this), ii);
    },

    sendIdleSignal: function(args) {
        // console.log("state:idle");
        document.fire('state:idle');
        this.active = false;
        clearTimeout(this.timer);
    },

    sendActiveSignal: function() {
        if(!this.active){
            // console.log("state:active");
            document.fire('state:active');
            this.active = true;
            this.timer = setTimeout(this.sendIdleSignal.bind(this), this.idleInterval);
        }
    }
});

Then I just created another class that has the following somewhere in it:

Event.observe(document, 'state:idle', your-on-idle-functionality);
Event.observe(document, 'state:active', your-on-active-functionality)
yalestar
+1  A: 

This is what I've come up with. It seems to work in most browsers, but I want to be sure it will work everywhere, all the time:

var timeoutTime = 1800000;
var timeoutTimer = setTimeout(ShowTimeOutWarning, timeoutTime);
$(document).ready(function() {
    $('body').bind('mousedown keydown', function(event) {
        clearTimeout(timeoutTimer);
        timeoutTimer = setTimeout(ShowTimeOutWarning, timeoutTime);
    });
});

Anyone see any problems?

Darryl Hein
One little problem - you'll want to clear the existing timer before starting another one! I've edited this change in...
Shog9
Thxs Shog9, I've often wondered this. But, would it not clear the existing timer by overwriting variable with a new timer?
Darryl Hein
No, the variable is just a timer ID, the timer itself is an internal browser structure. Storing the ID in a variable is just a simple way to identify the internal browser structure to the clearTimeout function.
Breton
+1  A: 

If it's a security issue, doing this clientside with javascript is absolutely the wrong end of the pipe to be performing this check. The user could easily have javascript disabled: what does your application do then? What if the user closes their browser before the timeout. do they ever get logged out?

Most serverside frameworks have some kind of session timeout setting for logins. Just use that and save yourself the engineering work.

You can't rely on the assumption that people cannot log in without javascript, therefore the user has javascript. Such an assumption is no deterrent to any determined, or even modestly educated attacker.

Using javascript for this is like a security guard handing customers the key to the bank vault. The only way it works is on faith.

Please believe me when I say that using javascript in this way (and requiring javascript for logins!!) is an incredibly thick skulled way to engineer any kind of web app.

Breton
A comment though. If the application is using some sort of timed ajax requests to keep the information up to date, this may update your 'session timeout' on the server. So tracking your click events and sending this information with your ajax request parameters would make the "detection" of an idle state easier.
gnarf
Yes that's a fair comment. My main point is that the gatekeeper should be on the server, not the client. Supplimentary information from the client might be useful, but it's important to plan for failure, and not come to depend on client side code for security.
Breton
A: 

Without using JS, a simpler (and safer) way would simply be to have a lastActivity timestamp stored with the user's session and checking it on page load. Assuming you are using PHP (you can easily redo this code for another platform):

if(($_SESSION['lastAct'] + 1800) < time()) {
    unset($_SESSION);
    session_destroy();
    header('Location: session_timeout_message.php');
    exit;
}

$_SESSION['lastAct'] = time();

and add this in your page (optional, the user will be logged out regardless of if the page is refreshed or not (as he logs out on next page log)).

<meta http-equiv="refresh" content="1801;" />
Andrew Moore
Yes, I already have this. Remember, only a warning to help out the user so they don't loose information. This is not the authentication process for the site!!
Darryl Hein
In wish case just calculate the time since the last request (if you are not using Ajax, this is simple. If not, you'll need to update a variable everytime an ajax request is made). If no request has been made in the last, let's say 20 minutes, display a warning saying that their session expires in 10 minutes.
Andrew Moore