Home > jquery > jQuery idleTimer plugin

jQuery idleTimer plugin

There are a few cases where you want to know if the user is idle. Namely:

  • You want to preload more assets
  • You want to grab their attention to pull them back
  • You want close their banking session after 5 minutes of inactivity. (Jerk!)
  • You want the site to sneak off the screen and see if they notice ;-)

Nick Zakas wrote a script for YUI3 to handle these cases. His writeup has a great description of the architecture approach he took to the script.

In my jQuery adaptation, I did a few different things:

  1. Leveraged event namespaces for easy unbinding
  2. Considered mousewheel as activity, in addition to keyboard and mouse movement.
  3. Gave it a bit more jQuery-ish of an API

To use:

// idleTimer() takes an optional argument that defines the idle timeout
// timeout is in milliseconds; defaults to 30000
$.idleTimer(10000);
 
 
$(document).bind("idle.idleTimer", function(){
 // function you want to fire when the user goes idle
});
 
 
$(document).bind("active.idleTimer", function(){
 // function you want to fire when the user becomes active again
});
 
// pass the string 'destroy' to stop the timer
$.idleTimer('destroy');

Get the source on github (4.6k unminified)
View the demo

Note: If you want to change the timeout interval, you'll have to destroy the existing timer first.

2009.09.22: I've updated the idleTimer script for a few more features…
// you can query if the user is idle or not with data()
$.data(document,'idleTimer');  // 'idle'  or 'active'
 
// you can get time elapsed since user when idle/active
$.idleTimer('getElapsedTime'); // time since state change in ms

You can get the latest code, naturally, on github.

2010.05.11: Due to popular demand, there is now support for multiple timers!!

Why would you want this? Lets say you want ..

  • One Timer to restore the forms, messages boxes, etc.

  • Another Timer of a different length timeout to notify about expiration sessions.
// bind to specific elements, allows for multiple timer instances
$(elem).idleTimer(30*1000);
$(elem).idleTimer('destroy');
$(elem).idleTimer('getElapsedTime'); // some ms something
 
$.data(elem,'idleTimer');  // 'idle'  or 'active'

Notice this new API is on $.fn.idleTimer, as in it works on a jQuery collection instead of just the jQuery global object.

If you're using this along with the old $.idleTimer api, you should not do $(document).idleTimer(...)

All these element-bound timers will only watch for events inside of them. You may just want to watch page-level activity, in which case you may set up your timers on document, document.documentElement, and document.body. Those will allow three separate timers that will catch all page activity.

Again, check out the demo or view the source on github. :)

2011.03.11 : You should check out Eric Hynds' Idle Timeout plugin. It improves a good bit on this guy here.

Paul Irish jquery

  1. Nate
    June 3rd, 2009 at 20:03 #1

    This looks very useful. You should add the destroy trick to infinite scroll.

  2. June 3rd, 2009 at 20:05 #2

    @Nate, I'm about to do some infinite scroll upgrades, and destroy is certainly a good call. Thx Nate!

  3. SlexAxton
    June 3rd, 2009 at 23:54 #3

    Great work Paul. I'm gonna find a reason to use this nice and soon.

  4. Einar
    June 4th, 2009 at 04:47 #4

    A tiny non–issue — it doesn't register me as active on just a mouse click, without moving the mouse.

  5. June 4th, 2009 at 08:17 #5

    @Einar, Aye. I suppose a click should be included. (Though it'd be hard to click without mouse movement, eh) But the overhead is pretty minimal. :)

    FWIW it currently watches these events: 'mousemove keydown DOMMouseScroll mousewheel'. I'll throw click in, too.

  6. June 4th, 2009 at 10:12 #6

    Very nice, I can see this being used for a web-based chat script.

  7. Kris
    June 4th, 2009 at 10:14 #7

    Thanks a ton! I have been thinking about something like this for a while now.

  8. June 4th, 2009 at 10:18 #8

    Great i found my solution..

  9. June 5th, 2009 at 16:00 #9

    Hi I'm using this plugin and it's working very well except for one problem. It seems to be interfering with the jQuery ui dialog feature. When I include $.idleTimer(120000) in my script firebug shows a 'too much recursion' error when I attempt to close an open modal dialog. When I remove that line of code it works fine. Any ideas? Thanks in advance.

  10. June 5th, 2009 at 17:57 #10

    @Marc Thanks for the bug report Marc. It looks to be interfering with any unbind() calls that use namespaces. In this case: $(document).unbind('.dialog-overlay')

    Turns out that $(foo).bind('click.bar mousewheel.bar ',fn) causes a recursive loop in $.event.remove(). {There's an extra space}

    Gave it a quick $.trim() and we're back in business.

    Committed to github with version 0.6.080605.

  11. June 5th, 2009 at 18:06 #11

    Wow amazingly quick response thanks – Got everything fixed before I could even get back from work! @Paul Irish

  12. ezod
    June 9th, 2009 at 10:59 #12

    Hi,
    great plugin, but I think there´s a little bug (tested on the demo site):

    If you are idle and simply click the mousebutton (without moving the mouse) the "active.idleTimer" is fired but the idle timer doesn´t work anymore ;)

    greetz
    ezod

  13. June 9th, 2009 at 13:52 #13

    @ezod Good catch. The script didn't restart the timeout after it changed states. (It would be waiting for the next user event.)

    Fixed and updated everywhere as ver 0.7.080609.

  14. June 15th, 2009 at 03:08 #14

    $.idleTimer('destroy');
    Don't forget this!

  15. Joel Holder
    June 24th, 2009 at 13:30 #15

    Looks as though idleTimer doesn't pickup movements over an iframe. When I have idleTimer setup on a parent page and I move my cursor just within the boundary of an iframe on page, the idle.idleTimer still fires its function, then immediately fires active.idleTimer.

  16. pisces
    July 24th, 2009 at 18:45 #16

    Hi
    I am trying to use the idle timer for implementing Session time out in my application.
    My concern is that if user opens multiple browser windows for the same session(ctrl+N for IE6) and is active on one of the windows the user shouldnt time out
    Is there any way I can use this timer to implement this?

    I was thinking of having a session cookie and updating the last hit time whenever the user goes active on any window.
    Then Check if the time out is reached by comparing the last hit with the current time at periodic intervals of idle time .

    My application is a sessionless application .
    Pls suggest some possible way.

  17. August 3rd, 2009 at 16:59 #17

    @pisces Your cookie technique seems reasonable. Probably the best way to handle that I can think of at the moment.

  18. ezhh
    August 6th, 2009 at 03:17 #18

    Thank you very much!!!
    Very nice and useful.
    1 minute and dream become true =)

  19. Aury
    August 17th, 2009 at 00:04 #19

    This is great, but it doesn't work if the user is moving the mouse/using the keyboard in an embedded flash file. I don't suppose there's a workaround for that?

  20. August 17th, 2009 at 21:14 #20

    @Aury Newp. Flash and iframes are the two obvious areas where this fails.

  21. agask2
    August 26th, 2009 at 14:53 #21

    Aury seems right is there any way to get this to detect movement in side of a particular frame. Eg the javascript is on the main page then i have 3 frames It would be nice to monitor the one frame but so far no go. Ive tried $(frames["content"].document).bind( and it appears it only works when transitioning or moving over one frame to another not just sitting in the same frame?? any ideas?

  22. August 26th, 2009 at 20:08 #22

    @agask2 You'd have to have this script running in each frame, and then tell the top frame the results via window.postMessage() or this sort of thing.

    Not terribly trivial.

    // this is half of what you'd need...
    $(document).bind("idle.idleTimer", function(){
     $.postMessage('idle down here!',top.location.href);
    }).bind('active.idleTimer',function(){
     $.postMessage('active down here!',top.location.href);
    });
  23. agask2
    August 27th, 2009 at 09:01 #23

    @Paul Irish
    thanks good input! But my problem is I do not know what will be the src for the frame will be. So I cannot put javascript there,It could be google or anything really. I just need the javascript on the outside to montior what is inside the frame for movement. Any ideas there??

  24. August 27th, 2009 at 09:16 #24

    This is exactly what I was looking for.
    Thanks, Paul :)

  25. August 27th, 2009 at 09:42 #25

    @agask2
    Frames are tricky and they don't bubble up things like mousemovement to the parent frame. And then due to the Same Origin Policy, you can't inject script into them at will. So you're in a tough place. :)

    One technique is to run the pages through a local proxy and inject script into them. That sounds like not fun, though. :/

  26. agask2
    August 27th, 2009 at 10:19 #26

    @Paul Irish
    ouch.. yah, I do not foresee that as being an option really. Can you inject script if everything is on the same domain?? maybe? without a proxy? Thanks for all your support thus far. Its a real nice script for a single page.

  27. August 27th, 2009 at 11:17 #27

    @agask2 If it's on the same domain you could probably hook into it… i think..

    // no guarantees this would work. i'm just guessing
    $($('#iframeid').contents()[0].document).bind("idle.idleTimer", function(){
     top.$(top.document).triggger('idle.idleTimer');
    });
  28. Frank Watts
    September 3rd, 2009 at 04:00 #28

    Silly question, but how can I call the static method isIdle?

    $.idleTimer.isIdle is undefined

  29. September 3rd, 2009 at 11:07 #29

    @Frank Watts ,

    If you want to just indicate (externallY) that the user is idle, you can trigger the custom event:

      $(document).trigger('idle.idleTimer');
  30. Frank Watts
    September 3rd, 2009 at 18:35 #30

    I don't want to trigger it, I want to see what the current status is. The reason is because I have another timer running that will autoscroll a page every 15 minutes. I don't want to autoscroll if the user is not idle.

  31. September 22nd, 2009 at 18:43 #31

    I've updated the script to allow you to query if the user is currently idle or not (through data())

    And to get the elapsed time since the last state change. (If you want to say a user has been idle for x seconds….)

  32. Maikel
    September 23rd, 2009 at 17:09 #32

    It would be great if you could to attach several idleTimer to the document.

    cheers

  33. September 23rd, 2009 at 21:08 #33

    @Maikel

    You indicated the use-case for this would be
    * One Timer to restore the forms, messages boxes, etc.
    * Another Timer to notify about expiration sessions.

    It's a good idea. If other people think it would be useful, I'll add it to the script. (Basically it would bind/trigger on individual elements instead of just the document.)

  34. October 22nd, 2009 at 11:17 #34

    Is there a way to pop up a warning of a pending timeout?
    Like this:
    You're session is about to end, click here to continue browsing.

  35. November 3rd, 2009 at 10:32 #35

    For those wondering about implementing expiring sessions/pending timeout, I wrote a blog post on the subject: http://www.erichynds.com/blog/?p=66

  36. S Kraggerud
    December 22nd, 2009 at 04:59 #36

    If you could implement functionality so we could use several idleTimers that would be great!

  37. Vincent Stoessel
    December 22nd, 2009 at 14:33 #37

    I second the call for a version that can do multiple timers. I have a 5 minute and 20 second timeout warning, and this would be great for that.
    Great work!

  38. Sandeep
    January 11th, 2010 at 16:51 #38

    @Paul Irish
    $(document).trigger('idle.idleTimer') is not working. It does not execute the function which i have set for idle.idleTimer.

  39. January 21st, 2010 at 18:14 #39

    will try immediately, thanx for the functions…

  40. Derek
    February 1st, 2010 at 16:07 #40

    Hey, thanks for this idle timer, it works great. I just can't figure out how to use getElapsedTime. Can it be used to find out how long the user was idle for once they become active again? If so, how?

  41. February 2nd, 2010 at 08:36 #41

    Very useful. Thanks! One question, how exactly do you use the query feature?

    // you can query if the user is idle or not with data()
    $.data(document,'idleTimer'); // 'idle' or 'active'

  42. vijay patil
    February 26th, 2010 at 01:05 #42

    hi paul great script , how to include this script onto php code . i m developing a tracking application in php where in after logging in , suppose he remains out for a minute , it should change to idle state , well how to use your script in my php code .. am newbie in jquery .. Please Help me …
    Thanku in adv …

  43. Y2J
    March 25th, 2010 at 20:46 #43

    Paul its a very usefule script. But I think this plugin does not work with IE8/Safari. If the mouse/cursor is left on the screen the idle event is raised for a second and goes back to active event. But this works perfectly fine in firefox. Any idea why?

  44. April 12th, 2010 at 10:05 #44

    Wouldn't $(window).blur(…) be considered idle time too? Any chance of you adding this? On my site at http://i-emote.appspot.com I use the jQuery Timer plugin to do periodic page updates. When the focus shifts off of the document (window) I stop the timer which effectually halts page updating. I'd like to do the same thing using idle time and if your plugin encapsulated the logic for window.blur and window.focus it would serve all my usecases.

    Also, I wasn't able to find this plugin on jQuery's site where they list all available plugins. It would be great to see it listed there as it obviously holds a lot of value for other jQuery users.

    Thanks for providing this great plugin to the jQuery community.

    Jeff

  45. Dave
    May 1st, 2010 at 15:04 #45

    @Paul Irish a BIG +1 for this feature

  46. Dave
    May 1st, 2010 at 15:06 #46

    I mean a +1 for multiple timers on a page feature

  47. May 11th, 2010 at 23:14 #47

    Hey guys! I added support for multiple timers! :)
    http://paulirish.com/2009/jquery-idletimer-plugin/#multiple

  48. May 12th, 2010 at 11:53 #48

    Thanks Paul this is very useful, the multiple timer is the way to go!

  49. Maikel
    May 12th, 2010 at 18:36 #49

    @Paul Irish
    Great! I will try it As soon as possible.

    Cheers

  50. Chris
    May 13th, 2010 at 12:04 #50

    Your example page has an error. http://paulirish.com/demo/idle-timer

    Error: obj is null
    Source File: http://github.com/paulirish/jquery-idletimer/raw/master/jquery.idle-timer.js
    Line: 106

    Browser: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)

  51. May 13th, 2010 at 12:14 #51

    @Chris
    Thanks! fixed now.

    read more on the bug that caused this.. The Mysterious Firefox setTimeout "Lateness" argument™

  52. PIerre
    May 14th, 2010 at 16:15 #52

    Is there a way to detect if the user is idle in any tabs of the browser.
    For example, if he is working in another tab, then the event would not be trigger.

    Thanks.

  53. dmtr
    May 19th, 2010 at 16:45 #53

    There seems to be an inconsistency. User can enter the 'active' state only from the 'idle' state and not from the default 'null' state.

    You can check it on your demo – open the page and keep moving the mouse – boxes will never get yellow highlight.

  54. dmtr
    May 19th, 2010 at 17:09 #54

    Another inconsistency is that the user is assumed to be 'active for the first x seconds', this is certainly true for the document, but not so for the document elements.

  55. Jake
    June 25th, 2010 at 05:29 #55

    Just to let you know, all of the internal functions (toggleIdState, stop, handleUserEvent) appear to leak into the global scope.

  56. Malathi
    August 2nd, 2010 at 17:32 #56

    Hi Paul,
    Its a nice plugin.Very useful. Thanks.
    Using jQuery idleTimer plugin v0.8.092209 have an issue in Google Chrome(v5.0.375.125):
    In Chrome, for the below scenarios say:
    1)Whenever the user enters an invalid input an alert box pops up and when the user click "Ok" button
    2)When the user clicks on the Print pop up "Ok" button
    it considers as if the system is in idle state & redirects the user to Login Page.
    It works fine in all the above cases with other browsers.
    Any idea ? Kindly share your thoughts.

    -Thanks

  57. Quinton Pike
    September 3rd, 2010 at 18:23 #57

    For some reason in Chrome, i had the idle timer setup and working fine, but when i do an alert(adsffd); after I click 'ok' and the popup goes away, it triggers the idletimer and asks if Im still active.

    It does not happen in FF, only Chrome (5.0.375.127) Windows 7 pro.

    Any help would be great, I even tried destroying before the alert and then re-initiating it after but it doesnt seem to work after that.

  58. September 10th, 2010 at 15:11 #58

    I also have the issue with Chrome in version 6.0.472.55 and version 7.0.520.0

    Works fine in IE and FF though.

  59. September 10th, 2010 at 15:27 #59

    Sorry guys but i'm not gonna have time to dig into this…

    Would love some help if you want to help track down exactly why this is happening..

    All the code is right here.
    http://github.com/paulirish/jquery-idletimer/

  60. sme
    September 24th, 2010 at 09:45 #60

    I am trying to use your plugin to poll against a cookie. How would I reset the inactivity timer each time it times out?

  61. sme
    September 24th, 2010 at 09:53 #61

    nevermind. I was calling destroy and then setting the timeout. This was not working. walked away for a couple minutes. looked at the code again and removed the destroy line. works as desired. thanks for the plugin.

  62. philk
    November 12th, 2010 at 03:39 #62

    Great plugin.

    It seems once you destroyed a timer, it cannot be reactivated?
    Given this code, which display a "screen lock" div:

    $.idleTimer(2 * 1000);

    var unlockKeys = [], unlockKeySequences = ["131,133", "27,13"];
    $(document).bind("idle.idleTimer", function() {
    $.idleTimer('destroy');
    $('#lock').show().text(navigator.userAgent);
    $(document).keydown(function(e) {
    unlockKeys.push(e.keyCode);
    $(" + unlockKeys.toString() + ").appendTo('#lock');
    var s = unlockKeys.toString();
    if (s.indexOf(unlockKeySequences[0]) >= 0 || s.indexOf(unlockKeySequences[1]) >= 0) {
    $(document).unbind('keydown', arguments.callee);
    $('#lock').hide();
    $.idleTimer(2 * 1000);
    }
    });
    });

    The idle event is only fired once.

  63. November 15th, 2010 at 23:30 #63
  64. December 2nd, 2010 at 17:56 #64

    @Paul Irish
    I don't know about the root cause or how to fix it, it has something to do with alert/confirm mucking with timeouts (see more discussion here) but I've found the workaround to be checking the idle state in the idle event:

    $(document).bind("idle.idleTimer", function(state){
     if(state === 'idle'){
      // code here...
     }
    });
  65. December 2nd, 2010 at 20:42 #65

    @T.J. Barbour
    Scratch that, this doesn't fix it, I failed to regression test it, plese ignore!
    Sorry!

  66. guru katre
    December 18th, 2010 at 04:00 #66

    Hi Paul,

    Thanks for this blog, i used given code. it is very help full
    this is very good functionality. which help people to make very interactive and intelligent site.

  67. December 21st, 2010 at 07:05 #67

    Great blog and post. This post in fact just saved my life in a project i'm working on.

  68. Octavient
    January 9th, 2011 at 12:06 #68

    Has somebody solved this with frames on the same domain? I need to make the parent document listen to mouse/keyboard activity within a child frame, and execute the script in the parent document as normal (i.e., when the user is idle in both documents, the timeout script executes in the parent document; user activity in either document tells the script that the user is active).

    Will pay for a functioning solution.

    Paul suggested the solution was something along these lines…

    // no guarantees this would work. i'm just guessing
    $($('#iframeid').contents()[0].document).bind("idle.idleTimer", function(){
    top.$(top.document).triggger('idle.idleTimer');
    });

  69. Young
    February 6th, 2011 at 17:43 #69

    @T.J. Barbour

    Here's my work around. In the toggleIndleSTate function, replace the "// reset timeout counter" section with the following:

    // reset timeout counter
    var elapsed = (+new Date) – f.olddate;
    f.olddate = +new Date;

    // handle Chrome always triggering idle after js alert or comfirm popup
    if (idle && (elapsed < timeout)) {
    idle = false;
    return;
    }

  70. Young
    February 7th, 2011 at 22:06 #70

    @Young

    Slight adjustment to address the issue that the idletimeout may not start if there is no activity after a page loads:

    // reset timeout counter
    var elapsed = (+new Date) – f.olddate;
    f.olddate = +new Date;

    // handle Chrome always triggering idle after js alert or comfirm popup
    if (idle && (elapsed < timeout)) {
    idle = false;
    clearTimeout($.idleTimer.tId);
    if (enabled)
    $.idleTimer.tId = setTimeout(toggleIdleState, timeout);
    return;
    }

  71. Young
    February 7th, 2011 at 22:09 #71

    @Young

    Slight adjustment to what I have above to address issue where idletimeout may not start if there is no activity after a page loads:

    // reset timeout counter
    var elapsed = (+new Date) – f.olddate;
    f.olddate = +new Date;

    // handle Chrome always triggering idle after js alert or comfirm popup
    if (idle && (elapsed < timeout)) {
    idle = false;
    clearTimeout($.idleTimer.tId);
    if (enabled)
    $.idleTimer.tId = setTimeout(toggleIdleState, timeout);
    return;
    }

  72. Deva
    February 12th, 2011 at 10:04 #72

    Hi,

    Thank you for the excellent plugin. I want to logout the user if he is idle for 3 mins and if he is active, then the timer should reset and re-start counting..
    my code is

    $.idleTimer(180000);
    $(document).bind("idle.idleTimer", function() {
    window.location = "Login.aspx";
    });

    $(document).bind("active.idleTimer", function() {
    clearTimeout($.idleTimer.tId);
    });

    It seems like working. But Am I correct according to this code? Can you please reply so that i can use this good code in my project.

  73. April 1st, 2011 at 20:01 #73

    I integrated the fix in a fork on github at: http://github.com/zawaideh/jquery-idletimer

    You can grab it from there until it is pulled upstream.

  74. Malathi
    May 2nd, 2011 at 13:58 #74

    Many Thanks Zaid.Chrome alert issue got fixed in version 0.9.100511 & works fine.Thank you.

  75. Ketan Bhatt
    July 6th, 2011 at 23:11 #75

    I am facing the same issue on Safari browser. I have updated the code block to my JQuery file and it is working for Google Chrome but facing the issue for Safari. Any JS alert or confirm box close triggers JQuery Idletimer plugin.

  76. Ketan Bhatt
    July 13th, 2011 at 05:51 #76

    @Ketan Bhatt
    I am using Safari 5.0.5 Is anybody else who is facing similar issue?

  77. jamie
    July 22nd, 2011 at 09:24 #77

    Will this work with Iframes at all?
    I have a page with 3 html's. There is a main container, that holds 2 iframes ("Top" and "Bottom").

    I want to make it so that after 5 minutes of inactivity, iframe "bottom" will reload back to it's orignial source. (bottom.html)

    I can't seem to get it to work.

    This seems like it would be relatively simple but I am not too familiar with Iframes and I am wondering if this is why I can't get it to work?

    Thanks in advance!

  78. jamie
    July 22nd, 2011 at 09:26 #78

    Sorry, I should add, I will pay for a working solution for this.

    I know there are others that are having similar issues with Iframes but I haven't seen a solution posted.
    Thanks again!

  79. Brent
    August 9th, 2011 at 17:00 #79

    Jamie,

    This will work fine with iframes.
    In each of your iframe, put the following:

    handleUserEvent = function(){
    top.$(top.document).trigger('keydown.idleTimer');
    }

    var events = 'mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove'; // activity is one of these events
    $(document).bind($.trim((events+' ').split(' ').join('.idleTimer ')),handleUserEvent);

    This will simulate activity on the parent document when something happens in the iframe.
    You can then use the parent documents timer to control what happens to your iframe after the inactivity timeout

  80. Brent
    August 9th, 2011 at 17:38 #80

    @jamie
    Jamie,
    This will work fine with iframes.
    In each of your iframe, put the following:
    handleUserEvent = function(){
    top.$(top.document).trigger('keydown.idleTimer');
    }
    var events = 'mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove'; // activity is one of these events
    $(document).bind($.trim((events+' ').split(' ').join('.idleTimer ')),handleUserEvent);
    This will simulate activity on the parent document when something happens in the iframe.
    You can then use the parent documents timer to control what happens to your iframe after the inactivity timeout

  81. Mick Smith
    September 7th, 2011 at 04:01 #81

    Paul, well done very useful! Can I use this for free on my website which I charge people to use?

    I know it has Dual MIT & GPL License, but to be honest all the licensing terminology is all very confusing to me.

    Thanks

  82. September 7th, 2011 at 08:44 #82

    @Mick Smith
    Yup, you can do whatever you want with it :)

  83. Nagraj
    September 7th, 2011 at 23:22 #83

    Hi,
    i hav created one.html page inside body function i hav created iframe.

    and i given src=welcome.html. my prob is when page is idle for 5sec it shold show popup win. but when my mouse, key events not not detecting in iframe page means welcome.html page. it's detecting events oly in the one.html page. so pls give me solution.

  84. Nagraj
    September 8th, 2011 at 01:06 #84

    @Paul Irish

    Hey buddy

    i used ur code
    $(document).bind("active.idleTimer", function() {
    // function you want to fire when the user becomes active again

    // pass the string 'destroy' to stop the timer
    $.idleTimer('getElapsedTime');
    //clearTimeout($.idleTimer.tId);
    //$.idleTimer.tId = setTimeout(toggleIdleState, timeout);

    });
    it stops the dialog from generating but it doesn't restart the idletimer or should i say it doesn't reset the timer.
    how to call the
    $(document).ready(function()
    from another function

  85. Nagraj
    September 12th, 2011 at 02:32 #85

    @Brent
    Hi,
    i hav created one.html page inside body function i hav created iframe.

    and i given src=welcome.html. my prob is when page is idle for 5sec it shold show popup win. but when my mouse, key events not not detecting in iframe page means welcome.html page. it's detecting events oly in the one.html page. so pls give me solution.

  86. Nagraj
    September 12th, 2011 at 06:27 #86

    @Nagraj
    i hav created one.html page inside body function i hav created iframe.

    and i given src=welcome.html. my prob is when page is idle for 5sec it shold show popup win. but when my mouse, key events not not detecting in iframe page means welcome.html page. it's detecting events oly in the one.html page. so pls give me solution.

  87. Derek
    September 23rd, 2011 at 04:43 #87

    How would I use this to kick someone back to a page after a certain period of time… How can I do this?

  88. ramu
    October 2nd, 2011 at 22:50 #88

    hey paul,
    i want to see for how long a user was active..i want that value to inserted into the database..can we modify this program to do that??????

  89. Greg
    October 13th, 2011 at 06:14 #89

    Paul, thanks so much for this plugin. Works great, and it seems pretty light on processing considering the fact that 'active.idleTimer' triggers on mouse movement.

  90. Ras
    October 26th, 2011 at 03:55 #90

    Hi Paul,

    Thanks for the plugin. It works! But I encountered a bug in clearInterval and setInterval js functions. I fired a clearInterval function when the browser is in idle state and it works great, but when i fire a setInterval function whenever the browser is active, the setInterval function seems to iterate and produce a multiple setInterval functions everytime i enter an idle and active state. Please help. God bless!

  91. John Safar
    November 4th, 2011 at 09:58 #91

    I just tried out jQuery 1.7, and your plugin isn't working. I'm using it as a time out for session login.

  92. Greg
    November 7th, 2011 at 12:43 #92

    John Safar is right. I dug into the plugin, and found two things one can do to get at least a single, document-wide idle/active timer again.

    1) Replace the call to bind() in the plugin with a call to on(). Note that this is not required; however, it is future-proof.

    2) Remove the call to .stopPropagation in the declaration of the toggleIdleState function. Presumably, this may make it unfeasible to use multiple idle/active timers due to them bubbling up, but it will at least give you back the use of one for the entire document.

    Somehow, calling .stopPropagation right after the creation of the event seems to have broken its usage on the element to which it is bound. I figure this is an unforeseen bug in jQuery 1.7.

    Someone correct me if I'm wrong here.

  93. Dorababu
    November 22nd, 2011 at 07:51 #93

    How can i kill asp session when user was inactive

  94. December 13th, 2011 at 10:44 #94

    i am getting idle event, when mouse is active over a applet. How can this case be handled ?

  95. jamie
    December 22nd, 2011 at 11:40 #95

    @Brent

    Hey Brent, thank you for your help but I can still not get this to work.
    I have the reload script in index.html, that contains two other html's in Iframes (top.html in top Iframe and bottom.html and bottom Iframe).

    According to firebug, document. is my top.html as opposed to my index.html.

    I want to reload bottom, so i changed
    handleUserEvent = function(){
    top.$(top.document).trigger('keydown.idleTimer');
    }

    to

    handleUserEvent = function(){
    bottom.$(bottom.document).trigger('keydown.idleTimer');
    }

    but shouldn't this actually reference the index.html by using
    parent.document?

    bottom.document is showing as undefined for some reason.

  96. January 6th, 2012 at 05:58 #96

    Hi Paul,

    thank you for your good work! It makes it more fun

    I have one issue that I have sort of fixed but would like your view on: if a (mobile) device goes in sleep/suspend mode, the timer is also stopped. I use the timer to lock the screen after some time. However, if you have a timeout of, say, 10 min and the device goes to sleep after 5 min, then, if you wake it a day later, you can work on because the timeout has not fired. I now solve that by modifying your code to detect the time between user events. If that time is larger than the timeout I trigger the event. This is not ideal but better than nothing.

    regards,
    Mahatma

  97. Gary Hibbard
    January 11th, 2012 at 13:30 #97

    This looks like it's working for me to handle iFrames:

            //assign appropriate event handlers
            $(document).bind($.trim((events + ' ').split(' ').join('.idleTimer ')), handleUserEvent);
     
            $("iframe").each(function()
            {
                $(this).load(function()
                {
                    $($(this).contents()[0]).bind($.trim((events + ' ').split(' ').join('.idleTimer ')), handleUserEvent);
                });
            });
  1. June 4th, 2009 at 11:50 | #1
  2. June 4th, 2009 at 12:24 | #2
  3. June 5th, 2009 at 10:40 | #3
  4. June 5th, 2009 at 13:41 | #4
  5. June 6th, 2009 at 01:00 | #5
  6. June 6th, 2009 at 04:11 | #6
  7. June 7th, 2009 at 19:14 | #7
  8. September 4th, 2009 at 11:53 | #8
  9. October 14th, 2009 at 19:38 | #9
  10. January 23rd, 2010 at 05:59 | #10
  11. January 23rd, 2010 at 06:04 | #11
  12. April 12th, 2010 at 03:57 | #12
  13. April 12th, 2010 at 07:43 | #13
  14. March 26th, 2011 at 18:26 | #14
  15. August 22nd, 2011 at 17:03 | #15
  16. December 19th, 2011 at 01:28 | #16
  17. December 29th, 2011 at 08:34 | #17

For code blocks, use <pre lang="javascript">. css and html4strict are also accepted.

i left this space here for you to play. <3