需要在 30 秒后或关闭浏览器后清除会话并注销用户



code在MVC中,而且每个请求都使用ajax调用,因此URL没有变化。

使用以下代码,如果用户处于非活动状态 30 秒,我将能够执行注销用户的第一个操作,但在用户注销时无法执行操作。

<script>
    $(function () {
        $("body").on('click keypress', function () {
            ResetThisSession();
        });
    });
    var timeInSecondsAfterSessionOut = 30; // to change the session time out, change this value. Must be in seconds.
    var secondTick = 0;
    function ResetThisSession() {
        secondTick = 0;
    }
    function StartThisSessionTimer() {
        secondTick++;
        console.log(secondTick);
        if (secondTick > timeInSecondsAfterSessionOut) {
            clearTimeout(tick);
            window.location = '/Account/LogOff/0';
        }
        tick = setTimeout("StartThisSessionTimer()", 1000);
    }
    StartThisSessionTimer(); 
</script>

ALSE 我尝试了脚本的卸载或卸载前方法,但结果不符合预期。

如果用户在 30 秒内未执行任何操作或用户关闭浏览器,则需要注销用户。

提前谢谢。

正如我所看到的,var tick 是局部的,并且在每个时钟周期中都有定义,因此每秒调用超时:

function StartThisSessionTimer() {
             secondTick++;
             console.log(secondTick);
             if (secondTick > timeInSecondsAfterSessionOut) {
                 clearTimeout(tick);
                 window.location = '/Account/LogOff/0';
             }
             tick = setTimeout("StartThisSessionTimer()", 1000);
         }

尝试将超时的初始化分开,超出超时中继器的相同范围。告诉我们是否有用。

最新更新