I want to show user a popup if he has been there on a web page without doing any mouse clicks / keyboard press / mouse scrolling
Is there any way I can track this using javascript?
I want to show user a popup if he has been there on a web page without doing any mouse clicks / keyboard press / mouse scrolling
Is there any way I can track this using javascript?
var timeout = false;
function checkActivity() {
clearTimeout(timeout);
timeout = setTimeout(function () { alert('inactive'); }, AmountOfTime);
}
document.addEventListener('keydown', checkActivity);
document.addEventListener('mousedown', checkActivity);
document.addEventListener('mousemove', checkActivity);
checkActivity();
What this does is run a function after AmountOfTime milliseconds. It is reset on any user activity on the document.
You can handle Keyboard Events and Mouse Events in JavaScript.
You can have a timer counting down to when the user should be shown that message, and reset the timer whenever you get an eligible event.
You can use this (library)[https://github.com/mikesherov/jquery-idletimer] to detect, if the user is idle.
Example:
$(document).on('idle.idleTimer', function() {
//do something on idle;
});
$(document).on('active.idleTimer', function() {
//so something on activity;
});
$(document).idleTimer(1000, {
startImmediately: false,
idle: true,
enabled: true,
events: 'keydown' //there are more events in the docs;
});