超时后销毁会话

时间:2013-09-09 15:21:32

标签: javascript asp-classic session-timeout

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

Create.asp

<%

Session.Contents.RemoveAll()

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

%>

Time.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 %>

1 个答案:

答案 0 :(得分:0)

似乎需要额外的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秒。遗憾。