下面的代码在大部分情况下都有效,但我想知道是否可以对其进行一些调整。如果在 x 毫秒内没有鼠标活动,则会显示一个弹出窗口,提示您将被注销。然后,如果/当您单击“确定”按钮时,脚本将自动将您带到注销文件。
但是,如果在 x 毫秒后未单击“确定”按钮,我还想将屏幕带到 logout.php 文件。有谁知道我如何使用下面的代码来做到这一点? 谢谢
// 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;
}
从概念上讲,您一次只需要运行 1 个计时器。 一个计时器运行 14 分钟,另一个计时器运行一分钟(总共 15 分钟)。 一旦 14 分钟计时器用完,杀死它,然后启动 1 分钟计时器。 如果一分钟计时器用完,请注销用户。 如果用户按下“保持登录”按钮,则终止 1 分钟计时器并重新启动 14 分钟计时器。 冲洗并重复。
我尽我所能改变了你的代码。 希望你明白这一点。
// 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;
}
更新@VtoCorleone 的答案:
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();
});
我必须为我们的项目执行相同的功能。 使用以下代码:-
<script>
$(document).click(function(){
if(typeof timeOutObj != "undefined") {
clearTimeout(timeOutObj);
}
timeOutObj = setTimeout(function(){
localStorage.clear();
window.location = "/";
}, 1200000); //will expire after twenty minutes
});
</script>
每次我们点击屏幕上的任意位置,上面的代码都会设置一个计时器。 如果我们不单击它,它会自动注销到主屏幕。
<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>
只需使用此代码。
var timeoutTimer;
var expireTime = 1000*60*30;
function expireSession(){
clearTimeout(timeoutTimer);
timeoutTimer = setTimeout("IdleTimeout()", expireTime);
}
function IdleTimeout() {
localStorage.setItem("logoutMessage", true);
window.location.href="{{url('logout')}}";
}
$(document).on('click mousemove scroll', function() {
expireSession();
});
expireSession();
您可以直接使用下面的代码,
*注意:最后将“SSOLogout()”调用替换为您自己的注销函数。
let timer = 0;
let currSeconds = 0;
// Logout functionality after 2 hours of keeping screen idle
const expireTime = 7200; // 2 hours in seconds
// List of events that should reset the timer
const events = ['load', 'mousemove', 'mousedown', 'touchstart', 'click', 'keypress'];
// Attach resetTimer to each event
events.forEach(event => window.addEventListener(event, resetTimer));
function resetTimer() {
// Clear the previous interval
clearInterval(timer);
// Reset the seconds of the timer
currSeconds = 0;
// Set a new interval
timer = setInterval(startIdleTimer, 1000);
}
function startIdleTimer() {
// Increment the timer seconds
console.log(currSeconds); // For debugging, can be removed
currSeconds++;
if (currSeconds >= expireTime) {
SSOLogout(); // Call your logout function
}
}
function SSOLogout() {
console.log("User has been logged out due to inactivity.");
// Add your logout logic here
}
// Set the initial timer
resetTimer();