Detecting If the User is Idle or Inactive

by kirupa | 11 September 2013

Some apps require your constant attention. Such apps often include games, media players, anything that is CPU/battery intensive, and so on. For these kinds of apps, it may be important (as well as user-friendly) to do something when a user is no longer actively interacting with your app.

This is a companion discussion topic for the original entry at


It’s a very good Script.

I’m using this script in my website for checking user activity.


Hi this is a great script thanks heaps!
I want to use on a page that contains an iframe, but I noticed that if the user clicks or hover within the iframe, it doesn’t register the movements and therefore go inactive no matter what,
Any workaround to have this script register mouse moves, clicks or touchevents ( ipads) on iframes?

Jane - does this help?

I am not in front of a computer to verify, but it seems like it would work :slight_smile:

thanks, it would… but my iframe is from a different domain so I am running into a cross domain security[ issue. I have managed to adapt your script and have it half work. The timer starts ticking once an activity is detected on the iframe, but the timer doesn’t reset if the user becomes inactive. So once an activity is detected the page will refresh after 7 mins
here it is if you want to have a look at it or if it can be useful for anyone

    var timeoutID;
    var myConfObj = {
  iframeMouseOver : false

function startTimer() {
    // wait 2 seconds before calling goInactive
    timeoutID = window.setTimeout(goInactive, 70000);
function goInactive() {
function goActive() {
    // do something 
function resetTimer(e) {

  else {

   myConfObj.iframeMouseOver = true;


   myConfObj.iframeMouseOver = true; 

   myConfObj.iframeMouseOver = true; 

    myConfObj.iframeMouseOver = false;


Sorry for the delay in getting back to you. I’ll look into this tomorrow-ish. I’m personally interested in figuring this out as well, for it’s an interesting problem :slight_smile:

I don’t think there’s a way to have both of best world’s - interaction within a x-domain iframe and detection of that interaction in your frame without the other frame’s cooperation. If you want to capture events on your end, you can use an overlay for that, but then you lose interaction within the frame itself. And when interacting with the x-frame you’re locked out of knowing what that interaction is.

1 Like

That makes sense. With WebView-like elements in app frameworks/languages/etc. (Electron, Android, iOS, Windows, etc.) there are ways to inject scripts that you can then call from your outer app frame. I was hoping that something similar can be done using iframes! :stuck_out_tongue:

You can - using a browser extension. But this would require users of your site to install that extension (not unlike the requirement to install your app)

1 Like

Ok, but, if a user is seeing a embbeded video, like youtube or facebook?

You can always disable the “idle detection” functionality when a video is playing :slight_smile: