Javascript autorun code

I have the code below that works for the most part, but I wonder if it can be tweaked a bit. So what the following code does, if they are not mouse activity for x milliseconds, a pop-up window is displayed saying that they will be logged out, and then when / if you end up clicking the ok button, the script will automatically lead you to the exit file.

However, I would like to do this if the ok button is not pressed after x is the number of milliseconds to continue and bring the screen to the logout.php file. Does anyone know how I can do this using the code below? Thanks

// Set timeout variables. var timoutWarning = 840000; // Display warning in 14 Mins. var timoutNow = 100000; // Timeout in 15 mins would be 900000. var logoutUrl = 'logout.php'; // URL to logout page. var warningTimer; var timeoutTimer; // Start timers. function StartTimers() { warningTimer = setTimeout("IdleWarning()", timoutWarning); timeoutTimer = setTimeout("IdleTimeout()", timoutNow); } // Reset timers. function ResetTimers() { clearTimeout(warningTimer); clearTimeout(timeoutTimer); StartTimers(); $("#timeout").dialog('close'); } // Show idle timeout warning dialog. function IdleWarning() { // $("#timeout").dialog({ //modal: true alert("Warning, your page will redirected to login page. Due to not move your mouse within the page in 15 minutes."); //}); } // Logout the user. function IdleTimeout() { window.location = logoutUrl; } 
+8
javascript
source share
4 answers

It is clear that you only need one timer at a time. One timer that runs for 14 minutes and another for another minute (15 minutes in total). As soon as the 14-minute timer ends, kill it, and then start the 1-minute timer. If this minute timer ends, record the user. If the user clicks the "Stay logged in" button, kill the 1-minute timer and restart the 14-minute timer. Rinse and repeat.

I changed my code as much as I could. I hope you understand.

 // Set timeout variables. var timoutWarning = 840000; // Display warning in 14 Mins. var timoutNow = 60000; // Warning has been shown, give the user 1 minute to interact var logoutUrl = 'logout.php'; // URL to logout page. var warningTimer; var timeoutTimer; // Start warning timer. function StartWarningTimer() { warningTimer = setTimeout("IdleWarning()", timoutWarning); } // Reset timers. function ResetTimeOutTimer() { clearTimeout(timeoutTimer); StartWarningTimer(); $("#timeout").dialog('close'); } // Show idle timeout warning dialog. function IdleWarning() { clearTimeout(warningTimer); timeoutTimer = setTimeout("IdleTimeout()", timoutNow); $("#timeout").dialog({ modal: true }); // Add code in the #timeout element to call ResetTimeOutTimer() if // the "Stay Logged In" button is clicked } // Logout the user. function IdleTimeout() { window.location = logoutUrl; } 
+15
source share
  <script type="text/javascript"> var IDLE_TIMEOUT = 10; //seconds var _idleSecondsCounter = 0; document.onclick = function() { _idleSecondsCounter = 0; }; document.onmousemove = function() { _idleSecondsCounter = 0; }; document.onkeypress = function() { _idleSecondsCounter = 0; }; window.setInterval(CheckIdleTime, 1000); function CheckIdleTime() { _idleSecondsCounter++; var oPanel = document.getElementById("SecondsUntilExpire"); if (oPanel) oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + ""; if (_idleSecondsCounter >= IDLE_TIMEOUT) { //alert("Time expired!"); document.location.href = "logout.php"; } } </script> 
+1
source share

I needed to do the same functionality for our project. The following code is used: -

  <script> $(document).click(function(){ if(typeof timeOutObj != "undefined") { clearTimeout(timeOutObj); } timeOutObj = setTimeout(function(){ localStorage.clear(); window.location = "/"; }, 1200000); //will expire after twenty minutes }); </script> 

The code above will set a timer every time we click anywhere on the screen. If we do not click, it will automatically exit to the main screen.

0
source share

upgrade to @VtoCorleone answer:

 var warningTimeout = 840000; var timoutNow = 60000; var warningTimerID,timeoutTimerID; function startTimer() { // window.setTimeout returns an Id that can be used to start and stop a timer warningTimerID = window.setTimeout(warningInactive, warningTimeout); } function warningInactive() { window.clearTimeout(warningTimerID); timeoutTimerID = window.setTimeout(IdleTimeout, timoutNow); $('#modalAutoLogout').modal('show'); } function resetTimer() { window.clearTimeout(timeoutTimerID); window.clearTimeout(warningTimerID); startTimer(); } // Logout the user. function IdleTimeout() { document.getElementById('logout-form').submit(); } function setupTimers () { document.addEventListener("mousemove", resetTimer, false); document.addEventListener("mousedown", resetTimer, false); document.addEventListener("keypress", resetTimer, false); document.addEventListener("touchmove", resetTimer, false); document.addEventListener("onscroll", resetTimer, false); startTimer(); } $(document).on('click','#btnStayLoggedIn',function(){ resetTimer(); $('#modalAutoLogout').modal('hide'); }); $(document).ready(function(){ setupTimers(); }); 
0
source share

All Articles