简体   繁体   中英

Destroy session after timeout

I want to automatically destroy the session after 1 minute. but session.timeout is not working for me. Please review my code.

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

It appears to need an extra 15 seconds, but I'm not sure why. Set the starting to to 75 seconds and it should work.

A few other things:

  • runat="server" only works for ASP.Net applications. It is ignored for classic asp.
  • You should use SetInterval rather than repeated calls to SetTimeout
  • You can't necessarily rely on the timing of SetTimeout or SetInterval to be perfect for comparison. A better way is to compare date timestamps.

Here is the function adjusted for all of this, and enhanced to show one decimal place also

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

Again, I have no idea why there is an extra 15 seconds before the Classic ASP Session Timeout actually invokes. Sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM