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
(Partially inspired by the good core logic of Equiman earlier in this thread.)
sessionExpiration.js
sessionExpiration.js is lightweight yet effective and customizable. Once implemented, use in just one row:
This is an example of what it looks like in action, if you don't change the CSS.
I think this jquery code is perfect one , though copied and modified from above answers!! donot forgot to include jquery library in your file!
Without using jQuery, only JavaScript:
Credits: http://forums.devshed.com/javascript-development-115/alert-time-inactivity-click-logout-501444.html
You can add more DOM events if you need to. Most used are:
DOM Events list: http://www.w3schools.com/jsref/dom_obj_event.asp
Remember use
window
, ordocument
according your needs. Here you can see the differences between them: What is the difference between window, screen, and document in Javascript?I have created a small lib that does this a year ago:
https://github.com/shawnmclean/Idle.js
Description:
Visual Studio users can get it from NuGet by:
PM> Install-Package Idle.js
Here is a simple script using JQuery that handles mousemove and keypress events. If the time expires, the page reload.
All the previous answers have an always-active mousemove handler. If the handler is jQuery, the additional processing jQuery performs can add up. Especially if the user is using a gaming mouse, as many as 500 events per second can occur.
This solution avoids handling every mousemove event. This result in a small timing error, but which you can adjust to your need.
http://jsfiddle.net/q8wLuLbw/