【问题标题】:Display warning after X time of inactivity and logout after X+Y time javascript在 X 时间不活动后显示警告并在 X+Y 时间后注销 javascript
【发布时间】:2017-07-19 07:17:29
【问题描述】:

我已经看到了很多类似这个问题的答案。没有一个帮助我解决了这个问题。 问题- 我想在 5 分钟不活动后显示警告消息,如果用户在 5 分钟警告后仍然不活动,页面应自动重定向到 logoff.jsp。 我不想在这里使用 Jquery。它只能使用 Javascript 来完成。 救命!!

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";
        }

    }
 }

【问题讨论】:

  • 那是什么问题
  • 如果用户还没有选择 OK 或 Cancel 选项,页面应该在 5 分钟后自动重定向。

标签: javascript jsp user-interface session-timeout


【解决方案1】:

啊,您不能以编程方式关闭 alert()confirm() 或这些模式。如果你想这样做,你必须制作自己的盒子。

当您显示confirm() 时,您的 JS 执行将自动暂停。因此,您需要使用 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);

【讨论】:

  • 谢谢莫汉!我们可以使用 Jquery 来解决这个问题吗?我仍然是 Jquery 的初学者。如果是这样,我们该怎么办?
  • 与其说是回答不如说是评论吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
相关资源
最近更新 更多