首页 > 解决方案 > 当其中一个选项卡中有活动时,使所有选项卡的会话保持活动状态

问题描述

当我在浏览器中打开了几个选项卡并且其中一个选项卡处于不活动状态时,尽管其他选项卡有活动,但所有选项卡的会话都将关闭。

我在 _layout.cshtml 中找到的代码如下:

<script>
var inactivityTime = function () {

    var timeForCloseSession= '@Configuration.GetSection("Time")["Logout"]';
    var timeForOpenModalWarning = '@Configuration.GetSection("Time")["WarningModal"]';
    var time;
    
    window.onload = resetTimer;
    document.onmousemove = resetTimer;
    document.onkeypress = resetTimer;

    function logout() {
        $('#userInactive').modal('show');

        // We remove the user from the session
        setTimeout(closeSession, timeForCloseSession)
    };

    function closeSession() {
        if (!$('#userInactive').hasClass('show')) {
            // We do not close session if the modal is closed
            return false;
        } else {
            location.href = '/Login/Logout';
        }
    };

    function resetTimer() {
        clearTimeout(time);
        time = setTimeout(logout, timeForOpenModalWarning)
    }
};

window.onload = function () {
    console.log("We load the inactivity module");
    inactivityTime();
}

</script>

如果其中任何一个选项卡有活动,我希望能够使所有选项卡的会话保持活动状态非常感谢!

标签: javascriptc#.net

解决方案


推荐阅读