X时间不活动后显示警告,X + Y时间后注销退出javascript
我已经看到很多与此问题类似的答案.没有人帮助我解决这个问题. 问题-我想在5分钟不活动后显示警告消息,如果用户在5分钟警告后仍然不活动,则Page应该自动重定向到logoff.jsp. 我不想在这里使用Jquery.它只能使用Javascript完成. 帮助!
I have seen many answers similar to this question.None helped me resolving this. Issue- I want to display a warning message after 5 minutes of inactivity,If the user is still inactive post 5 minutes of warning, Page should automatically redirect to logoff.jsp. I dont want to use Jquery here.It has to be done using Javascript only. Help!!
var IDLE_TIMEOUT = 15; //seconds
var _idleSecondsCounter = 0;
document.onkeypress = function()
{
_idleSecondsCounter = 0;
};
document.onclick = function()
{
_idleSecondsCounter = 0;
};
document.onmousemove = 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) {
var answer = confirm("It's been long time you are idle.\nPlease click OK to continue session.\nTo close this session click Cancel.");
if (answer)
{
//document.location.href = "logout.html";
_idleSecondsCounter=0;
}
else{
//window.open('', '_self', ''); window.close();
document.location.href = "logoff.jsp";
}
}
}
啊,您不能以编程方式关闭alert()
或confirm()
或这些模式.如果要这样做,您必须自己制作一个盒子.
Ah, you cannot programmatically close alert()
or confirm()
or these modals. You've to make your own kind of box if you want to do that.
当显示confirm()
时,JS执行将自动暂停.因此,您需要使用HTML/CSS创建自定义对话框.
When you show confirm()
, your JS execution is automatically paused. So you'll need to create your custom dialog box using HTML/CSS.
function tempAlert(msg,duration)
{
var el = document.createElement("div");
el.setAttribute("style","position:absolute;top:40%;left:20%;background-color:white;");
el.innerHTML = msg;
setTimeout(function(){
el.parentNode.removeChild(el);
// 5 minutes over. sign off user
},duration);
document.body.appendChild(el);
}
tempAlert("We're signing you off in 5 minutes",5*60000);