3

I want to detect if a user is away from computer (i.e not doing any mouse movement or typing anything).

I want to do that with JavaScript.

Is this even possible?

On my page I want to fire a piece of code of JavaScript if the user is away.

I know there are blur, focus events but they are dependent on tab visibility. I don't want that dependency. Irrespective of the tab is opened or closes I want to detect mouse movement/keyboard inactivity on my page.

Raghav
  • 8,772
  • 6
  • 82
  • 106
  • 4
    Possible duplicate of [Detecting idle time in JavaScript elegantly](http://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly) – War10ck Apr 26 '16 at 20:36
  • Another possible solution: https://github.com/kidh0/jquery.idle. – War10ck Apr 26 '16 at 20:45
  • 2
    In general, Javascript can't detect anything outside its own page, and definitely not activity outside the browser. – Barmar Apr 26 '16 at 21:15

2 Answers2

3

There are also lots of great tools out there to detect browser's activity. here are a few:

https://github.com/arthurakay/ExtJS-Activity-Monitor

https://github.com/kidh0/jquery.idle

according to this last one, you only need jQuery and the following code:

$(document).idle({
  onIdle: function(){
    alert('Since you waited so long, the answer to the Ultimate Question of Life, the Universe, and Everything is 42');
  },
  idle: 10000
})

you can also use events and attach them to the body tag and catch all the events with native js. (it's just an idea).

RicardoE
  • 1,665
  • 6
  • 24
  • 42
  • Is this limited to detecting interaction with the just browser though? this other question suggests its impossible to detect generic interaction with the machine: https://stackoverflow.com/questions/27351628/how-to-detect-key-and-mouse-event-on-system-any-where-using-jquery-or-jsp is that correct? – MetaStack Jan 06 '23 at 00:11
-3

Programs are probably the better way to go about this.

The user could be at their computer, but be interacting with another program on their computer, and your web app would not even know because their mouse, keyboard, etc would not be interacting with the browser window. This is why web applications have timeouts.

In general, the best way to tell if a user is away from their computer is to set a timeout for inactivity via JavaScript.

For example:

setTimeout(function(){ alert("Hello"); }, 3000);
Retro Gamer
  • 1,096
  • 1
  • 10
  • 24
  • 2
    This isn't meant to sound snarky, but this is only a restatement of what the OP has already said: _"On my page I want to fire a piece of code of JavaScript if the user is away."_... As such it's more of a comment or statement than an answer. – War10ck Apr 26 '16 at 20:42
  • 2
    This *could* be a good answer if you showed **how** to set a timeout for inactivity via JavaScript. – JJJ Apr 26 '16 at 20:44
  • @Juhana I second that. It's not so much a bad answer. Just an incomplete answer. An example of how to do this will probably result in a few upvotes... – War10ck Apr 26 '16 at 20:46
  • Thanks for the suggestions everybody. I have updated my answer. – Retro Gamer Apr 26 '16 at 20:59
  • The code doesn't make any sense. How does triggering an alert after 3 seconds help detecting if the user is away from the computer? – JJJ Apr 26 '16 at 21:10
  • This is just an example of the syntax. Modify it as you wish for your application. – Retro Gamer Apr 26 '16 at 21:12
  • 2
    The question is how to detect that the user is not on their computer. Just saying "use timeout" is not helpful at all. It's like someone asking how to calculate 2+3 and your answer is "use a pencil". – JJJ Apr 26 '16 at 21:20