46

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.

12
  • Logging people out for just changing the tabs is probably a bad idea - what if they needed to go to another tab or program to get information to enter in to your form, and come back to find out they've been logged out? You should first consider if this is completely necessary before proceeding further. Commented Jun 17, 2009 at 20:26
  • 1
    It is completely necessary. Yes, it is annoying, but necessary because we are dealing with emergency response data (ie, gas pipeline explodes) and we need to keep the data safe and unfortunately the user's aren't always on secure machines (location wise). We can't have people sitting down and using someone else's account. Commented Jun 17, 2009 at 20:30
  • 2
    Okay, but don't forget that JS isn't reliable for most secure tasks, as almost all browsers have a mechanism to simply disable it. You'll really want to figure out a maximum time to sit on a page, then every time a page is loaded, store the timestamp somewhere, then every action server side should be checked against this before doing anything. Commented Jun 17, 2009 at 20:34
  • 1
    Okay...yes I am sending password through SSL. LOL. I am purely asking about timing the user out and detecting if they are actively using the site. And yes, the server will also be checking to ensure they are still logged in--it's still doing the full authentication process. Commented Jun 17, 2009 at 20:44
  • 1
    Again, this is purely to determine if they are idle. It has nothing to do with the actual authentication. Commented Jun 18, 2009 at 4:07

12 Answers 12

36

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?

Sign up to request clarification or add additional context in comments.

5 Comments

One little problem - you'll want to clear the existing timer before starting another one! I've edited this change in...
Thxs Shog9, I've often wondered this. But, would it not clear the existing timer by overwriting variable with a new timer?
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.
Just one addition, I added the "touchstart" event to work better on touch enabled devices. i.e. $('body').bind('mousedown keydown touchstart', function(event) {....
To add to Glade's comment, I also use mouse move, ala: $('body').bind('mousemove mousedown keydown touchstart', function(event) {....
22

Ifvisible.js is a crossbrowser lightweight solution that does just that. It can detect when the user switches to another tab and back to the current tab. It can also detect when the user goes idle and becomes active again. It's pretty flexible.

2 Comments

best answer for me (:
best solution for me.
19

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)

9 Comments

also you can look at the keyup event
The OP said not clicking and not typing, so there'd be no keyup event.
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
@Malfist-exactly what I meant, to check for both, not just one. that is why I said 'also look at the keyup event'
@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...
|
16

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)

Comments

10

Ifvisible is a nice JS lib to check user inactivity.

ifvisible.setIdleDuration(120); // Page will become idle after 120 seconds

ifvisible.on("idle", function(){
   // do something
});

Comments

3

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.

Comments

3

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.
}

5 Comments

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?
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- live() is for looking for new dynamically created elements
How are you resetting the timer? The setTimeout is just creating another timeout everytime the event happens.
@Darryl: checkActivity is just running periodically. the actual work of checking whether 30 minutes has passsed is done by comparing currentTime with lastActivityDateTime
2

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.

2 Comments

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.
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.
1

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;" /> 

3 Comments

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!!
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.
Refreshing the page automatically is not a good practice, as the user can be editing something and will lose the changes. You can better send an AJAX request to check the session every while.
1

You can add and remove classes to the document depending on the user active status.

// If the window is focused, a mouse wheel or touchmove event is detected
$(window).on('focus wheel touchmove', function() {
  $( 'html' ).addClass('active').removeClass('inactive');
});

// If the window losses focus
$(window).on('blur', function() {
  $( 'html' ).addClass('inactive').removeClass('active');
});

After that, you can check every while if the html has the "active" class and send an AJAX request to check the session status and perform the action you need:

setInterval( function() {
  if ( $( 'html' ).hasClass('active') ) {
  //Send ajax request to check the session
    $.ajax({
      //your parameters here
    });
  }
}, 60000 ); /* loops every minute */

Comments

0

If your concern is the lost of information for the user after a login timeout; another option would be to simply store all the posted information upon the opening of a new session (a new session will always be started when the older session has been closed/scrapped by the server) when the request to a page is made before re-routing to the logging page. If the user successfully login, then you can use this saved information to return the user back to where he was. This way, even if the user walk away a few hours, he can always return back to where he was after a successful login; without losing any information.

This require more work by the programmer but it's a great feature totally appreciated by the users. They especially appreciate the fact that they can fully concentrate about what they have to do without stressing out about potentially losing their information every 30 minutes or so.

Comments

-1

A global inactivity timer is set in the master page to monitor user engagement. The timer is configured to trigger every 20 minutes of inactivity. When user activity is detected, such as mouse movement, keypress, or interaction the existing timeout is cleared and a new one is started, effectively resetting the inactivity countdown.

The HandleInactivity function can be customized to perform actions such as extending the session, showing a warning, or initiating a logout.

var _inactivityTimeout;
var _inactivityDuration = 20 * 60 * 1000; // 20 minutes timer

function ResetInactivityTimer() {
    clearTimeout(_inactivityTimeout);
    _inactivityTimeout = setTimeout(HandleInactivity, _inactivityDuration);
}

function HandleInactivity() {
    //// This function will be called after 20 minutes of inactivity
    alert("You have been inactive for 20 mins");
    //your logic to handle inactivity, like logging out the user or showing a warning
}

// Bind events to reset the timer on user activity
$(document).on('mousemove keydown scroll click touchstart', function () {
    ResetInactivityTimer();
});

One important caveat is handling scenarios where certain functions or loops run for more than 20 minutes without direct user interaction. These cases must be treated separately to avoid false positives for inactivity.

This can be achieved in two different ways:

  1. Loop-Aware Activity Flagging

    Introduce a hidden <input> element (or similar DOM marker) with a specific class name (e.g., .long-process-flag).

    During long-running loops or iterations, update or monitor this element to signal that the browser is actively processing, even if the user is idle.

    The inactivity handler should check for the presence or state of this marker before triggering any inactivity-related actions (e.g., session timeout warnings).

    
    function HandleInactivity() {
      const isLongProcessActive = document.querySelector('.long-process-flag')?.value === 'active';
      if (isLongProcessActive) return; // Skip inactivity handling
      // Proceed with warning/logout/session extension
    }
    
    
  2. Distributed Invocation of ResetInactivityTimer

    Make the ResetInactivityTimer function globally accessible across all child pages or modules.

    Within each long-running loop or process, periodically invoke ResetInactivityTimer to reset the inactivity timer manually.

    This ensures that the session remains active as long as the system is performing meaningful work, even without user input.

    for (let i = 0; i < largeDataSet.length; i++) {
      processItem(largeDataSet[i]);
      if (i % 1000 === 0) {
        window.ResetInactivityTimer(); // Reset timer manually
      }
    }
    
    

2 Comments

"functions or loops run for more than 20 minutes without direct user interaction." Any web page that does this would generally be considered horribly misdesigned, and the inactivity timer would be the least of its problems.
You're absolutely right. While it's not a common practice to operate this way, I have come across a few internal applications that utilize APIs from the same domain, which allows them to bypass server-side calls when using AJAX. In such cases, executing large data sets through client-side APIs can lead to prolonged loop execution times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.