Page Session Countdown Timer

M

MC

I would like to display a timer in the corner of the page that shows the
user how many minutes:seconds till the session times out. Are there any good
examples out there?

Google has again failed me ;)

MC
 
E

Evertjan.

MC wrote on 22 mrt 2008 in comp.lang.javascript:
I would like to display a timer in the corner of the page that shows
the user how many minutes:seconds till the session times out. Are
there any good examples out there?

Session is a serverside concept.
Clientside, thee is not such a thing as a session timeout.
Google has again failed me ;)

Do you want Google to write your programming?
It will cost you!
 
M

MC

Evertjan,

I have to say your wrong. A javascript can track every time a page loads,
and knowing the session timeout, calculate an approximate, hopefully +- less
than one second, session timeout.

I would like to, given a timeout such as 30 minutes, have a countdown from
the onload event. It would then be nice to prompt the user to logout or
continue the session by sending a message back to the server to refresh the
session.

I say your wrong, because session timeouts ARE being done currently in many
websites such as banks. ...and I never suggested Google write my code, but
that Google search find an appropriate example.

Please troll someplace else if you have nothing constructive to add.

MC
 
S

sasuke

I would like to display a timer in the corner of the page that shows the
user how many minutes:seconds till the session times out. Are there any good
examples out there?

Google has again failed me ;)

MC

Here is a simple script which you can modify as per your needs with a
little bit of Javascript knowledge.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" />
<meta http-equiv="Script-Content-Type" content="text/javascript" /<title>Example</title>
<script type="text/javascript">
var t = 10; /* This would come from PHP */
var decr = 1;
var handle = null;
var e = null;
function startTimer() {
if(!e)
e = document.getElementById("time");
e.innerHTML = t;
handle = setInterval(function() {
if(t == 0) {
clearInterval(handle);
/* do what you want when the timer expires */
window.location.href = "http://www.google.com/";
}
else {
t -= decr;
e.innerHTML = t;
}
}, decr * 1000);
}
window.onload = startTimer;
</script>
</head>
<body>
<div>The time remaining is <span id="time"></span></div>
</body>
</html>

HTH
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
MC wrote on 22 mrt 2008 in comp.lang.javascript:


Session is a serverside concept.
Clientside, thee is not such a thing as a session timeout.

A client can count down for a length of time indicated by the server;
and can occasionally be synchronised with the server's opinion.

Try "Good countdowns" as your search string.
As a contrast, seek countdowns on .gov sites.

TD09690 : Until 03 September 2081 10:07:31 LCT : 26827d 11h 54m 56s .
 
E

Evertjan.

MC wrote on 22 mrt 2008 in comp.lang.javascript:
[Please do not toppost on usenet]
I have to say your wrong. A javascript can track every time a page
loads, and knowing the session timeout, calculate an approximate,
hopefully +- less than one second, session timeout.

No it cannot, as there could be another call in that session resetting
the sererside timeout counter.

Clientside cannot know that, unless it pols the server by ajax or alike
to ask the present value of the timeout counter. Under ASP it is not even
available, I suspect the same goes for PHP, but I don't know.
I would like to, given a timeout such as 30 minutes, have a countdown
from the onload event. It would then be nice to prompt the user to
logout or continue the session by sending a message back to the server
to refresh the session.

You cwertainly can do that, but it does not reflect the session state.
I say your wrong, because session timeouts ARE being done currently in
many websites such as banks.

Those sites heavily rely on serverside, I hope.
...and I never suggested Google write my
code, but that Google search find an appropriate example.

You did not suggest anything of the sort, though I might be what was in
your mind. You only said Google had failed you. If that was about
searching and not about the share value, it could also be that your
searching skills were not perfect. How would we know?
Please troll someplace else if you have nothing constructive to add.

So now you become abusive, thinking this NG is your personal helpdesk and
by asking Qs you have a right to dictate the answers?

My aswer was constructive, showing you you asked the wrong Q an prompting
you to a better one.

A countdown timer in Javascript, you simply could have asked for that,
is a simple affair, can be written within 10 minutes. But you did not ask
for that.

If you stay polite, maybe someone will help you.
 
M

MC

Sasuke,

Thank you. This should work quite nicely! I will be able to show the user
how much time is left on the page till timeout. I should be able to even
'show' a form saying they are going to time out in soon, and give them the
opportunity to refresh the session with a button there.

Thank you for helping!
MC
 
E

Evertjan.

Dr J R Stockton wrote on 22 mrt 2008 in comp.lang.javascript:
A client can count down for a length of time indicated by the server;
and can occasionally be synchronised with the server's opinion.

However, John, the server, under ASP at least,
does not know at the scripting level,
what the countdown status of the session timer is,
so much for the server's opinion.
 
J

Joost Diepenmaat

Evertjan. said:
However, John, the server, under ASP at least,
does not know at the scripting level,
what the countdown status of the session timer is,
so much for the server's opinion.

That can't be right. There may be no direct way of querying that info,
but one would assume the timeout is configurable (and so, knowable) and
reset at each page request. Which is all you'd need to know for doing
this fairly accurately.
 
M

MC

Joost,

Exactly what I was thinking...given a timeout such as 30 minutes, a client
page onload should be able to approximate the timeout. I have seen several
examples of this on bank sites. It doesn't matter if its ASP, JSP, PHP, or
CF. Just that the client is passed or stored in the page what the timeout
will be.

MC
 
S

sasuke

Sasuke,

Thank you. This should work quite nicely! I will be able to show the user
how much time is left on the page till timeout. I should be able to even
'show' a form saying they are going to time out in soon, and give them the
opportunity to refresh the session with a button there.

Thank you for helping!
MC

Glad I was able to help. Hope it all works out well for you. :)
 
E

Evertjan.

Joost Diepenmaat wrote on 23 mrt 2008 in comp.lang.javascript:
That can't be right.

But it is, Joost.
There may be no direct way of querying that info,
but one would assume the timeout is configurable (and so, knowable) and
reset at each page request. Which is all you'd need to know for doing
this fairly accurately.

Not fairly at all.

If the user does alt-N and has two duplicate pages, and clicks to another
site page from the second page 15 minutes later, the first page will show
the session is gone, while the session lives on happily [though not ever
after], and the session can be used from the page with the counter that
tells you it cannot.
 
J

Joost Diepenmaat

Evertjan. said:
Not fairly at all.

If the user does alt-N and has two duplicate pages, and clicks to another
site page from the second page 15 minutes later, the first page will show
the session is gone, while the session lives on happily [though not ever
after], and the session can be used from the page with the counter that
tells you it cannot.

Yeah that's true. There are ways of working around this problem, of
course. Provided you've got enough control of the session mechanism at
the server side you could just do a periodic Ajax request to check the
session status.

I still don't know what you mean about the *server* scripts not knowing
what the session timer is. Sounds like a really strange restriction to
me.
 
E

Evertjan.

Joost Diepenmaat wrote on 23 mrt 2008 in comp.lang.javascript:
Evertjan. said:
Not fairly at all.

If the user does alt-N and has two duplicate pages, and clicks to
another site page from the second page 15 minutes later, the first
page will show the session is gone, while the session lives on
happily [though not ever after], and the session can be used from the
page with the counter that tells you it cannot.

Yeah that's true. There are ways of working around this problem, of
course. Provided you've got enough control of the session mechanism at
the server side you could just do a periodic Ajax request to check the
session status.

I still don't know what you mean about the *server* scripts not
knowing what the session timer is. Sounds like a really strange
restriction to me.

Ours is not to ask the why of the language/platform at implementation
time,
just to implement it.

ASP cannot read the actual state of the session timeout counter.
PSP probably has the same restriction.

The only time that you know it's state is at the reset moment.

You could test by Ajax if the session is still active,
but that would be done "destructively",
as the counter is reset by the test.

Kind of a quantum measurement dilemma, isn't it?
Provided you've got enough control of the session mechanism

You could forego the server internal session mechanism,
[which depends on a session cookie, btw],
and build your own session with a database.
Then you can read the last session reset time from that database.

That probably is the way some banks do their work, having an session-id
in the querystring, and no cookies necessary for that.

The above shows a timeout timer is not even necessary, only a comparison
of 'now' to the last resettime at every new html request [and some sort
of database garbage collection sql execution now and then].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top