How can i fire an event when the user is Idle - javascript

How can i fire a JQuery event when the user is gone on idle from the website for a specified time, and reset the timer on mouse move, scroll or on a key press.

This works by using a setTimeout function to fire at the end of the specified seconds. If basically anything happens during that time (the mouse moves, the page is scrolled, or a key is pressed) the timeout period is reset.
Set the idle period on the third line.
idleTimer = null;
idleState = false;
idleWait = 2000;
(function ($) {
$(document).ready(function () {
$('*').bind('mousemove keydown scroll', function () {
clearTimeout(idleTimer);
if (idleState == true) {
// Reactivated event
$("body").append("<p>Welcome Back.</p>");
}
idleState = false;
idleTimer = setTimeout(function () {
// Idle Event
$("body").append("<p>You've been idle for " + idleWait/1000 + " seconds.</p>");
idleState = true; }, idleWait);
});
$("body").trigger("mousemove");
});
}) (jQuery)

Here is a simple script using JQuery that handles mousemove and keypress events. If the time expires, the spinner-wrap event is fired.
<script type="text/javascript">
var idle = 0;
$(document).ready(function () {
//Increment the idle time counter every seconds.
var idleInterval = setInterval(timerIncrement, 1000); // 1 seconds
//Zero the idle timer on mouse move.
$(this).mousemove(function (e) {
idle = 0;
});
// zero the idle timer on keypress
$(this).keypress(function (e) {
idle = 0;
});
});
function timerIncrement() {
idle = idle + 1;
if (idle > 5) { // 5 seconds
$('.spinner-wrap').trigger('click');
}
}
</script>

Related

Countdown won't stop at 0, keeps decrementing

I have a timer that keeps decrementing after 0 so I get negative numbers. Time starts when a button is clicked and the pauses when the same button is clicked again.
button.addEventListener("click", function(event){
console.log('timer started')
// Timer paused, click to start
// Pause/Play Timer
if(timeleft == 90){
timeleft = setInterval(function() {
time--;
timerDisplay.innerHTML = time;
}, 1000);
} else if (timeleft === 0) {
clearInterval(timeleft);
} else{
// Timer running, click to pause
console.log('timer paused')
clearInterval(timeleft);
timeleft = 90;
}
});
The stop and start functionality works fine but why won't it stop counting down once it reaches 0?
Sample code for reference to build a timer.
const actionBtn = document.getElementById("start-timer");
let isTimerRunning = false;
let interval = null;
let maxTime = 20;
// Click handler to start and stop timer button in DOM
actionBtn.addEventListener("click", function() {
if(isTimerRunning) {
// stop timer
stopTimer();
} else {
// start timer
startTimer();
}
});
// Function to start timer
function startTimer() {
isTimerRunning = true; // toggle state, so on click again we can stop timer
// 1sec interval function to reduce max time
interval = setInterval(function() {
maxTime = maxTime - 1;
if(maxTime > -1) { // if the more time left (more than 0) update UI
renderUi(`Reset (${maxTime}sec)`);
} else { // if there is no time left stop timer
stopTimer();
}
}, 1000);
}
// stop timer
function stopTimer() {
isTimerRunning = false; // toggle state, so on click again we can start timer
clearInterval(interval); // clear the interval
maxTime = 20; // reset max time to initial value
renderUi(`Start Timer (${maxTime}sec)`); // update UI to initial value
}
// A generic function to update UI
// if we have complet UI structure we can update this function
function renderUi(str) {
actionBtn.innerText = str;
}
<button id="start-timer">Start Timer (20sec)</button>

Javascript - page reload script won't work

I'm a complete newb to javascript, and much of this code was pulled from other sites. I'm trying to use two things I found to make a page redirect after the user is inactive for a specified amount of time.
I was able to get the timer working and make the page reload instead of redirecting, but my redirect code doesn't work for some reason.
EDIT: forgot to mention this code needs to work for specific pages, as I will be using one page to redirect to a specific page, and another to a different page.
jQuery(document).ready(function( $ ){
var idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 10000); // 10 seconds
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
if ((idleTime > 0) && (window.location.pathname == '/wp')) { // 10 seconds
window.location.href = "https://www.google.com";
}
}
});
I tried you code, it works, but wrong. If it don't work for you - remove that && (window.location.pathname == '/wp') and try again. You have bigger problem, your code just redirects after 10 seconds no matter what. You need to replace to something like that:
jQuery(document).ready(function( $ ){
var idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 1000); // 1 second
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
if ((idleTime > 9) && (window.location.pathname == '/wp')) { // 10 seconds
window.location.href = "https://www.google.com";
}
}
});

Popup after user has been idle

I have written some code that brings up a message for the user to either ignore the message or go to another page if they have been idle for more than a minute. Everything works, as I want it to except when the user ignores the message. Here is my code:
if ( valid ) {
var idleTime = 0;
jQuery(document).ready(function () {
var idleInterval = setInterval(timerIncrement, 60000);
});
function resetTimer() {
idleTime = 0;
}
jQuery(document)
.on('mousemove', resetTimer)
.on('keydown', resetTimer)
.on('scroll', resetTimer);
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime >= 1) {
jQuery('#popup').show();
}
jQuery(window).unbind();
}
jQuery('#popupClose').click(function() {
jQuery('#popup').hide();
});
}
I want the popup to not repopulate after they click #popupClose.
I'd do it like this. Just define a start time when you initialize your script. Let an interval run that checks how much time has passed. If it's more than your time wished show the dialog. if not hide it. Also reset the timer on your events.
Your javascript will look like this
$('#btclose').on('click', function(){
clearInterval(interv);
$('#popup').hide();
});
var start = new Date();
var interv = setInterval(function(){
var now = new Date();
console.log()
if(now-start > 5*1000){
$('#popup').show();
}
},10);
$('body').on('mousedown click mousemove', function(){
start = new Date();
});
Here's my fiddle
https://jsfiddle.net/c2L7wpn3/8/
Seems to work. Let me know if this helps
You can either store the information in a cookie, or with a flag (depending on whether you want the popup on each pageview or only once, period).
Then, check the flag/cookie before the showing of the popup. For example:
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime >= 1) {
if (jQuery('#popup').data('closed') == 1){
jQuery('#popup').show();
}
}
jQuery(window).unbind();
}
jQuery('#popupClose').click(function() {
jQuery('#popup').data('closed', 1);
jQuery('#popup').hide();
});

How to reset session on keypress

I have implemented session timeout using setInterval() when the window loaded. How to reset session time on keypress event. Here is the code that I've written.
window.onload = function(){
(function(){
var counter = 60;
setInterval(function() {
counter--;
if (counter >= 0) {
//alert(counter)
span = document.getElementById("count");
span.innerHTML = counter;
}
if (counter === 0) {
$("#session-expired-modal").modal('show');
}
}, 1000);
})();
}
function sessionExpiredRedirect(){
window.location=url;
}
// Using jQuery (but could use pure JS with cross-browser event handlers):
var idleSeconds = 30;
$(function(){
var idleTimer;
function resetTimer(){
clearTimeout(idleTimer);
idleTimer = setTimeout(whenUserIdle,idleSeconds*1000);
}
$(document.body).bind('mousemove keydown click',resetTimer); //space separated events list that we want to monitor
resetTimer(); // Start the timer when the page loads
});
function whenUserIdle(){
//...your code
}

jquery to disable form elements

Scenario is that I have written a function for auto refresh on form element and I want onclick event on login button to disable the auto refresh function.
I'm not able to get disable the autorefresh and the page still refreshes.
My code is:
$('#form').ready(function () { //Increment the idle time counter every minute.
var idleInterval = setInterval("timerIncrement()", 15000); // 1 minute
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e){
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 2) { // 20 minutes
window.location.reload();
}
}
$('#login').ready(function () {
alert("working promptly");
//Zero the idle timer on mouse movement.
$(this).click(function (e) {
alert("Hi In click !");
$('#form').attr("disabled","true");
});
});
I guess you are needing to clear the interval timer:
$(this).click(function (e) {
alert("Hi In click !");
$('#form').attr("disabled","true");
clearInterval(idleInterval);
});

Categories