0

我想在 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 %>
4

1 回答 1

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);

同样,我不知道为什么在 Classic ASP Session Timeout 实际调用之前有额外的 15 秒。对不起。

于 2013-09-09T16:39:29.980 回答