javascript 为应用程序创建会话超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8257143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:48:35  来源:igfitidea点击:

Session Timeout creation for application

javascriptjsp

提问by Nagarajan

I have a requirement for session timeout. This is my requirement. Once logged in to my application, a time count sholud start. If it reaches 10 mins, it should gives a alert which prompting as

我有会话超时的要求。这是我的要求。一旦登录到我的应用程序,时间计数应该开始。如果达到 10 分钟,它应该发出警报,提示为

do you want to continue?

你要继续吗?

If customer press "Yes" the session should continue and time resetted as 0. If press "No", the session should close and error page need to come. I need this changes in javascript and jsp. Can any one have idea about this?

如果客户按“是”,会话应该继续并且时间被重置为 0。如果按“否”,会话应该关闭并且需要出现错误页面。我需要在 javascript 和 jsp 中进行此更改。任何人都可以对此有想法吗?

回答by Aleks G

In your frameset file, you can have this:

在您的框架集文件中,您可以拥有:

<script type="text/javascript">
    window.setTimeout('checkIfContinue()', 10*60*1000);  //10 minutes

    function checkIfContinue()
    {
        if(confirm("Do you want to continue?"))
        {
            window.setTimeout('checkIfContinue()', 10*60*1000);  //start the timer again
        }
        else
        {
            window.location = 'timeout.html';
        }
    }
</script>

Of course, if you need a fancy dialog box, then you'll need to code that instead of using javascript function confirm.

当然,如果您需要一个漂亮的对话框,那么您需要对其进行编码,而不是使用 javascript 函数confirm

回答by dnuttle

I'm not sure if you're looking for a complete code example. But briefly, I would say that you should use ajax on the client side to ping the server once a minute or so, to see if the timeout has expired. But then, if it has, there's a challenge. The server tells the client, "the session has expired," but then what? If the user doesn't respond with "yes," you want the session to timeout? If so, then how long does the server wait to hear back from the client before timing out? What if the user doesn't click "yes" for 30 seconds? So it can get sticky, and you would have to decide on the rules.

我不确定您是否正在寻找完整的代码示例。但简而言之,我会说您应该在客户端使用 ajax 每隔一分钟左右 ping 服务器一次,以查看超时是否已过期。但是,如果有,那就是一个挑战。服务器告诉客户端,“会话已经过期”,但是然后呢?如果用户没有响应“是”,您希望会话超时吗?如果是这样,那么服务器在超时之前等待多长时间收到来自客户端的回复?如果用户在 30 秒内没有点击“是”怎么办?所以它会变得很粘,你必须决定规则。

If no action is taken unless the user clicks "no" then this isn't a problem.

如果不采取任何行动,除非用户点击“否”,那么这不是问题。

However I should also say that I don't think it's a good idea to ask visitors if they want to continue. It makes visitors feel like they are being pestered to leave.

但是,我也应该说,我认为询问访问者是否要继续下去不是一个好主意。它让游客觉得他们被纠缠着离开。