Basically I wanted to lock screen if user did nothing from a time period. How to detect that user did nothing from a period of time. what will be the term I should search for?
3 Answers
When you say lock screen... Does it mean that the user will be logged out from the website and promoted with username & password? After some fixed time?
Then, this looks straight forward. Just set the cookie expiration to the expected number of minutes upto which he can considered to be in logged in state. So after expiration? If the user interacts he will taken to login screen.
Another solution:
You can save the last user interaction timing in a Javascript variable and then run another method in setinterval method to calculate time difference between last interaction and current time.. auto redirect if the value is greater than a limit.

- 1,319
- 1
- 13
- 24
bind some event (mouseover, mousemove, keydown, scroll etc.) to your body. So if user is doing anything then the event will be triggered.So when the event will be triggered then update some flag with the current time and configure the webworker which will listen on every 5 seconds(or you can use setInterval() method). In every 5 seconds the callback function will check whats the last time user did some action. If it is more than your timeout time then do screen lock or whatever action you want.

- 829
- 8
- 14
-
[Webworkers](http://caniuse.com/#feat=webworkers) are difficult and overkill for this functionality, and they are not compatible with `IE<11`. Other than that, it's a sound strategy. – Emil S. Jørgensen Jun 15 '16 at 07:10
-
Then setInterval is there in javascript, which will check in every x time interval and do the same. – isambitd Jun 15 '16 at 07:19
You can use Date
to mark a point in time and use that as a timer.
You can use addEventListener
to listen for events that would reset the timer.
At last your can set an interval
to test the difference between now and the Date
of the last user action.
//https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
var lastAction = new Date();
var lastActionInterval = setInterval(function(){
//if more than 5 seconds (5000 milliseconds)
if(new Date() - lastAction > 5 * 1000) {
//Act on timeout
alert("timeout!");
//Stop checking
clearInterval(lastActionInterval);
}
},1000); //Checks every second (1000 milliseconds)
function userDidSomething(){
lastAction = new Date();
}
//bind mousemove and keydown actions to reset our timer
window.addEventListener("mousemove", userDidSomething, false);
window.addEventListener("keydown", userDidSomething, false);
EDIT 1
A simpler method would be to use setTimeout
, and just reset it on a user action:
//https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
var userActionHandle;
function userDidSomething() {
clearTimeout(userActionHandle);
userActionHandle = setTimeout(function () {
alert("5 seconds of inactivity");
}, 5000);
}
window.addEventListener("mousemove", userDidSomething, false);
window.addEventListener("keydown", userDidSomething, false);
//Start timeout
userDidSomething();

- 6,216
- 1
- 15
- 28