JQuery - does not detect any action for minutes "x" and runs a function on this event (there is no event in X minutes)

How to detect that the user has done nothing for function "X" and the trigger function in this event?

eg.

if( // no action from user for 'X' minutes) { //do stuff } ??

Any suggestion is much appreciated.

+6
source share
2 answers

Live demo

Like everyone else, use setTimeout and set it to the right amount of time to wait. If there is any activity, just clear the timeout using clearTimeout .

 // If theres no activity for 5 seconds do something var activityTimeout = setTimeout(inActive, 5000); function resetActive(){ $(document.body).attr('class', 'active'); clearTimeout(activityTimeout); activityTimeout = setTimeout(inActive, 5000); } // No activity do something. function inActive(){ $(document.body).attr('class', 'inactive'); } // Check for mousemove, could add other events here such as checking for key presses ect. $(document).bind('mousemove', function(){resetActive()}); 
+14
source

Set timeout for X minutes.

In the <body> events of keydown and mousemove, clear the timeout and set it again.

+6
source

Source: https://habr.com/ru/post/894837/


All Articles