Is it possible to detect "idle" time in JavaScript?
My primary use case probably would be to pre-fetch or preload content.
Idle time: Period of user inactivity or without any CPU usage
Is it possible to detect "idle" time in JavaScript?
My primary use case probably would be to pre-fetch or preload content.
Idle time: Period of user inactivity or without any CPU usage
You could probably hack something together by detecting mouse movement on the body of the form and updating a global variable with the last movement time. You'd then need to have an interval timer running that periodically checks the last movement time and does something if it has been sufficiently long since the last mouse movement was detected.
Similar to Iconic's solution above (with jQuery custom event)...
Pure JavaScript with properly set reset time and bindings via
addEventListener
Here is an AngularJS service for accomplishing in Angular.
Keep in mind this idle checker will run for all routes, so it should be initialized in
.run()
on load of the angular app. Then you can useidleChecker.secondsIdle
inside each route.You can do it more elegantly with underscore and jquery-
Debounce actually a great idea! Here version for jQuery free projects: