【问题标题】:automatically logout from page自动退出页面
【发布时间】:2014-04-23 16:37:57
【问题描述】:

我想在用户有时空闲但无法执行之后自动执行。我使用了以下 javascript,但没有发生任何事情,我还在 Web 配置中添加了会话超时,但它也不起作用。请给我一些想法。

<script type="text/javascript">
        var timer1, timer2;
        document.onkeypress=resetTimer;
        document.onmousemove=resetTimer;
        function resetTimer()
        {
            document.getElementById('timeoutPopup').style.display='none';
            clearTimeout(timer1);
            clearTimeout(timer2);

            // waiting time in minutes
            var wait=10;

            // alert user one minute before
            timer1=setTimeout("alertUser()", (60000*wait)-1);

            // logout user
            timer2=setTimeout("logout()", 60000*wait);
        }

        function alertUser()
        {
            document.getElementById('timeoutPopup').style.display='block';
        }

        function logout()
        {
            window.location.href='Logout.aspx';
        }



}



        </script>

【问题讨论】:

    标签: javascript asp.net


    【解决方案1】:

    我认为您可以为此使用 html 元标记

    <meta http-equiv="refresh" content="30">
    

    这将导致您的页面在 30 秒后刷新(根据需要更改时间),刷新时您可以在服务器端检查会话并执行您想要的逻辑。

    【讨论】:

    • 我应该在哪里使用上面的标签?
    • 但是现在怎么知道pahe是否已经闲置了。
    • 这并不理想。即使用户正在做某事,它也会强制页面刷新,这很不方便,并且有丢失未保存的数据输入的风险。
    【解决方案2】:

    您可以通过在 web config 中添加以下代码来实现此目的:

     <system.web>
        <sessionState timeout="10"></sessionState>
     </system.web>
    

    这里 timeout=10 表示 10 分钟后,会话将过期,因此将完成自动注销

    【讨论】:

    • 这只会使会话超时,但不会发生实际注销(除非您使用会话发出身份验证请求)如果您正在使用表单身份验证,那么请将表单身份验证超时设置为 10并设置滑动过期=false stackoverflow.com/questions/17812994/…
    • 你能用代码解释一下吗。我真的不知道该怎么办?
    • 我已经这样做了,但它不工作..
    【解决方案3】:

    setTimeout 的第一个参数是函数句柄。 JS Timing

    <script type="text/javascript">
            var timer1, timer2;
            document.onkeypress=resetTimer;
            document.onmousemove=resetTimer;
            function resetTimer()
            {
               document.getElementById('timeoutPopup').style.display='none';
               clearTimeout(timer1);
               clearTimeout(timer2);
                    // waiting time in minutes
                var wait=10;
    
               // alert user one minute before
                timer1=setTimeout(alertUser, (60000*wait)-1);
    
                // logout user
                timer2=setTimeout(logout, 60000*wait);
            }
    
            function alertUser()
            {
                document.getElementById('timeoutPopup').style.display='block';
            }
    
            function logout()
            {
                window.location.href='Logout.aspx';
            }
    
    
    
    } </script>
    

    【讨论】:

      猜你喜欢
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2011-05-21
      • 1970-01-01
      相关资源
      最近更新 更多