简体   繁体   English

如何在JavaScript会话超时结束之前警告用户?

[英]How to warn user before session timeout ends in javascript?

I am coming to a minor problem where I have a session timeout in my web.xml for 15 minutes. 我遇到一个小问题,我的web.xml有一个会话超时15分钟。 But, in javascript I tried to warn the user for 5 minutes before it hits 15 minutes, but when it warns the user and the user clicks on OK, then the web app won't work anymore. 但是,在javascript中,我尝试警告用户5分钟,直到命中15分钟,但是当警告用户并且用户单击“确定”时,该Web应用程序将无法再工作。 All I want to do is warn the user about there session timeout and then when they press ok, they can still use the web app. 我要做的只是警告用户会话超时,然后当他们按OK时,他们仍然可以使用Web应用程序。 Yet, when there session timeout times up, I want them to click on OK and takes them to a different web page. 但是,当会话超时超时时,我希望他们单击​​“确定”并将其带到另一个网页。 Thank you for the help. 感谢您的帮助。

First things first: When you want to warn the user 5 minutes before the 15 minutes timeout, the warning shoud be displayed at 10 minutes after page load. 首先要注意的是:如果要在15分钟超时之前5分钟警告用户,则应在页面加载后10分钟显示警告。

setTimeout( function() { alert("Your session will expire in 5 minutes."); }, 10*60*1000);

If you want to redirect the user after 15 minutes to a different page, you don't actually need the user to confirm this. 如果要在15分钟后将用户重定向到另一个页面,则实际上不需要用户确认。

setTimeout(function() {
    window.location = "timeout.xhtml";
}, 15*60*1000); 

So, when does the timeout actually happen? 那么,超时实际上什么时候发生?

With multiple tabs your user could just use the app in 2 tabs. 使用多个标签,您的用户只需在2个标签中使用该应用即可。 The actions in the second tab will keep the app from timing out. 第二个标签中的操作将阻止应用程序超时。 One way is to reload the page and let the server handle the timeout and redirection part. 一种方法是重新加载页面,并让服务器处理超时和重定向部分。 Another option (preferred one) is to periodically check, whether the session still exists. 另一个选择(首选)是定期检查会话是否仍然存在。 Be aware: Your session checking should not extend the timeout period. 请注意:您的会话检查不应延长超时期限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM