Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to detect inactive or away user

Tags:

javascript

Is there any standard way of detecting when a user is away or not looking at the webpage? Something similar to this library.

For example, I want to trigger an event if there is no activity after some seconds and another event when the user change browser tab, minimize window or doesn't have the webpage in focus.

like image 583
Shawn Mclean Avatar asked Jan 26 '26 13:01

Shawn Mclean


1 Answers

I would add 'blur' and 'focus' handlers to the window to detect when the user changed tabs. On blur, the user is deemed inactive.

Then for actual activity I would attach mouseMove and keyDown event handlers to the document (capture phase mode), every time they fired I would reset a timer. When the timer elapsed the user is deemed to be inactive.

like image 163
Jim Blackler Avatar answered Jan 28 '26 03:01

Jim Blackler