jQuery 如何在用户在论坛上发帖时保持用户的会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3877821/
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-08-26 16:16:11  来源:igfitidea点击:

How to keep alive a user's session while they are posting on a forum?

jqueryhttpformssession-timeout

提问by Nick

I have a site with a session timeout of 15 minutes. On some pages a user occasionally spends longer than 15 minutes filling in a reply. What is the best solution to keep alive the session in this case?

我有一个会话超时为 15 分钟的站点。在某些页面上,用户有时会花费超过 15 分钟来填写回复。在这种情况下保持会话活动的最佳解决方案是什么?

I already use JQueryon these pages, so possibly pinging a server, but on what events?

我已经JQuery在这些页面上使用过,所以可能会 ping 服务器,但是在什么事件上?

The backend is a Strutson Tomcat.

后端是一个Strutson Tomcat

回答by Alexander Sagen

Given you don't want to change the site's session timeout..

鉴于您不想更改站点的会话超时..

Set a timeout/interval (< 15min) event in javascript and decide what to do when the event triggers. If you want the session to be active as long as the page is open, then fine, keep pinging every < 15 min. But that's probably not what you want as a user leaving a public computer should get logged out at some point.

在 javascript 中设置超时/间隔(< 15 分钟)事件并决定在事件触发时要做什么。如果您希望会话在页面打开时一直处于活动状态,那么很好,每隔 < 15 分钟保持 ping 一次。但这可能不是您想要的,因为离开公共计算机的用户应该在某个时候注销。

You could maintain a variable lastActivity, which is updated on each document mousemove or document keydown. If there's been any activity since last ping, ping again.

您可以维护一个变量lastActivity,该变量在每个文档 mousemove 或文档 keydown 上更新。如果自上次 ping 后有任何活动,请再次 ping。

To get more sophisticated, you could count the events and ping the server only if number of events > threshold at timeout.

为了变得更复杂,您可以仅在事件数> 超时阈值时对事件进行计数并ping 服务器。

The basic example:

基本示例:

setInterval(function(){
   $.get('/ImStillAlive.action');
}, 840000); // 14 mins * 60 * 1000

With basic check for typing activity:

对打字活动进行基本检查:

$(function(){
    var lastUpdate = 0;
    var checkInterval = setInterval(function(){
       if(new Date().getTime() - lastUpdate > 840000){
           clearInterval(checkInterval);
       }else{   
            $.get('/ImStillAlive.action');
       }
    }, 840000); // 14 mins * 60 * 1000

    $(document).keydown(function(){
         lastUpdate = new Date().getTime();
    });
});

回答by Phil LaNasa

I'm thinking that you really don't want this for ALL pages though. If you were to do this on every page, even the ones that really don't depend on session variables, you'd consume a lot of memory.

我认为你真的不希望所有页面都这样。如果您要在每个页面上都这样做,即使是那些真正不依赖会话变量的页面,您也会消耗大量内存。

I'd suggest throwing in a conditional to do it only on certain pages where it's necessary, i.e.

我建议只在某些必要的页面上添加条件,即

if (location.href.indexOf('/checkout') !== -1) {
setInterval(function(){
    $.get('/keepalive.action');
}, 840000); // 14 mins
}

回答by Alec

You could set an interval ever 5 minutes:

您可以每 5 分钟设置一次间隔:

setInterval(function() {
    // check if a reply textarea is present on the page and focussed
    // assuming it's e.g. a textarea with the id "reply"
    if ($('#reply:focussed').length) {
        // user is still on the reply page
        // call on a script on the server that renews the session
    }
}, 300000);

Or check whether he/she is still typing something:

或者检查他/她是否还在打字:

<textarea id="reply"></textarea>
<input type="hidden" id="reply_length" name="length" />
setInterval(function() {
    // get length of text in textarea
    var len = $('#reply').val().length;

    // get previous length
    var prev = $('#reply_length').val().length;

    // if they don't match, assume they're doing something
    if (len != prev) {
        // renew session
    }

    // update previous length
    $('#reply_length').val(len);
}, 300000);