javascript - 不活动后的 Javascript 重定向/注销(带有模式弹出窗口)
问题描述
以下 JS 不会自动重定向或注销,它只会在模态框上单击“确定”时重定向到 url。
// reset main timer i,e idle time to 0 on mouse move, keypress or reload
window.onload = reset_main_timer;
document.onmousemove = reset_main_timer;
document.onkeypress = reset_main_timer;
document.onscroll = reset_main_timer;
// create main_timer and sub_timer variable with 0 value, we will assign them setInterval object
var main_timer = 0;
var sub_timer = 0;
// this will ensure that timer will start only when user is loged in
var user_loged_in = $("#user_loged_in").val()
// within dilog_set_interval function we have created object of setInterval and assigned it to main_timer.
// within this we have again created an object of setInterval and assigned it to sub_timer. for the main_timer
// value is set to 15000000 i,e 25 minute.note that if subtimer repeat itself after 5 minute we set user_activity
// flag to inactive
function dialog_set_interval(){
main_timer = setInterval(function(){
if(user_loged_in == "true"){
$("#inactivity_warning").modal("show");
sub_timer = setInterval(function(){
$("#user_activity").val("inactive")
},3000);
}
},10000);
}
// maintimer is set to 0 by calling the clearInterval function. note that clearInterval function takes
// setInterval object as argument, which it then set to 0
function reset_main_timer(){
clearInterval(main_timer);
dialog_set_interval();
}
// logout user if user_activity flag set to inactive, when he click ok on popup. whenuser click O.K
// on the warning the subtimer is reset to 0
$(".inactivity_ok").click(function(){
clearInterval(sub_timer);
if($("#user_activity").val() == "inactive"){
window.location = window.location // if your application not manage session expire
//automatically. clear cookies and session her
}
});
<div id="inactivity_warning" class="modal hide fade" data-backdrop="static" style="top:30%">
<div class="modal-header">
<button type="button" class="close inactivity_ok" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="row-fluid">
<div id="custom_alert_message" class="span12">
You will be logged out in due to inactivity very shortly.
</div>
</div>
<div class="modal-footer">
<a href="javascript:void(0)" class="btn inactivity_ok" data-dismiss="modal" aria-hidden="true">O.K</a>
</div>
</div>
</div>
<input id="user_activity" name="user_activity" type="hidden" value="active" />
<input id="user_loged_in" name="user_loged_in" type="hidden" value="true" />
我希望模态显示为用户的警告消息..让他们知道他们将被注销...他们可以单击以留在页面上或忽略它,它将自动重定向到注销页面。
解决方案
编辑
现在除了 css 之外,你已经拥有了所有东西......不要开始按设计制作 webapp,没有人喜欢一个非常不工作的应用程序。
我使用了 jQuery,因为您似乎自己也在使用它。
userIsLogguedIn
没有实现,因为我不知道您验证用户的方式。
logout
也必须正确定义。
没有Ok keep me connected
,因为每个鼠标事件都会设置当前活动并隐藏模式。
$( document ).ready( initApp);
function initApp(){
console.debug('initApp');
const activityModal = $('#modal');
activityModal.hide();
const activityMaxDelayWarning = 5 * 1000;
const activityMaxDelay = 35 * 1000;
const activityCheckInterval = 5 * 1000;
activityRegister();
document.addEventListener('mousemove', activityRegister);
setInterval( activityCheck, activityCheckInterval);
function activityRegister() {
popupHide();
activityLast = Date.now();
console.debug('Activity set to', activityLast);
}
function userIsLogguedIn() {
//DO it
return true;
}
function activityCheck() {
const delay = Date.now() - activityLast;
console.debug("Activity delay is", delay);
if ( !userIsLogguedIn() ) {
console.debug(" user is not logged in");
return;
}
if ( delay > activityMaxDelay ) {
console.debug(" will logout");
logout();
} else if ( delay > activityMaxDelayWarning ) {
console.debug(" will show warning");
popupShow();
}
}
function popupShow(){
activityModal.show();
}
function popupHide(){
activityModal.hide();
}
function logout() {
window.location.href='https://stackoverflow.com/questions/65007832/javascript-redirect-logout-after-inactivity-with-modal-popup/65008157';
}
}
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body>
<div id="modal">
<p>You will be logged out in due to inactivity very shortly.</p>
</div>
<div>After some time, a warning message will appears ahead</div>
</body>
</html>
推荐阅读
- css - 不依赖 JavaScript 的 Bootstrap CDN 的 WordPress 后备?
- azure - 在 MacOS 上使用 sourcetree 连接到 Azure devops 存储库
- javascript - 修改对象后如何获取文本对象字体大小
- google-api - Google My Business API 中的 account_name 是什么?
- c# - XAML 到高质量位图
- sap-query - 选择后 SAP 信息集查询修改表
- regex - 正则表达式:第二组匹配太多
- extentreports - 范围报告 V4 覆盖测试结果
- java - 将几个 Java 循环替换为一个简单的 lambda,但看起来过于复杂
- javascript - 我正在尝试使用模式视图在 html 中显示两个不同的文本,但是 javascript 重叠并且无法正常工作