【问题标题】:Destroy session after timeout超时后销毁会话
【发布时间】:2013-09-09 15:21:32
【问题描述】:

我想在 1 分钟后自动销毁会话。但 session.timeout 对我不起作用。请检查我的代码。

创建.asp

<%

Session.Contents.RemoveAll()

Session("page") = "Active"
Session.timeout = 1
Response.Redirect "time.asp"

%>

时间.asp

<%

Response.Write(Session("page"))

if Session("page") = "Active" then 
Response.Write( "Session is Active,This Page will Expire After 1 mintue ") 

%>



<h2> Time Out: <span id="timerLabel" runat="server">65</span> </h2>

<script type="text/javascript">

    function countdown() 
    {
        seconds = document.getElementById("timerLabel").innerHTML;
        if (seconds > 0) {
            document.getElementById("timerLabel").innerHTML = seconds - 1;
            setTimeout("countdown()", 1000);
        } else { location.reload(); }
    }

    setTimeout("countdown()", 1000);

</script>

<% else %>

<a href='create.asp'> Click Here to Create Session </a>

<% End if %>

【问题讨论】:

    标签: javascript asp-classic session-timeout


    【解决方案1】:

    似乎需要额外的 15 秒,但我不知道为什么。将开始时间设置为 75 秒,它应该可以工作。

    其他一些事情:

    • runat="server" 仅适用于 ASP.Net 应用程序。对于经典的 asp,它会被忽略。
    • 您应该使用SetInterval 而不是重复调用SetTimeout
    • 您不一定要依赖SetTimeoutSetInterval 的时间来进行比较。更好的方法是比较日期时间戳。

    这是针对所有这些调整的功能,并增强了显示一位小数

    var countDownSeconds = 75;
    document.getElementById("timerLabel").innerHTML = countDownSeconds;
    var start = new Date().getTime();
    
    function countdown() {
        var elapsedSeconds = (new Date().getTime() - start) / 1000;
        var secondsRemaining = countDownSeconds - elapsedSeconds;
        if (secondsRemaining > 0) {
            document.getElementById("timerLabel").innerHTML = secondsRemaining.toFixed(1);
        } else {
            location.reload();
        }
    }
    
    setInterval("countdown()", 100);
    

    再次,我不知道为什么在经典 ASP 会话超时实际调用之前有额外的 15 秒。对不起。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2011-11-25
      • 1970-01-01
      • 2016-04-01
      • 2011-12-12
      • 1970-01-01
      相关资源
      最近更新 更多