setTimeout - clearTimeout - Why Doesn't This Work?

J

joey.powell

Hey guys I have some js code as follows...

-------

var SessionTimer;

function StartSessionTimer()
{
SessionTimer = setTimeout('RedirectToSessionTimedOutPage(),60000)
}

function RestartSessionTimer()
{
clearTimeout(SessionTimer);
StartSessionTimer();
}

function RedirectToSessionTimedOutPage()
{
window.location = '/SessionTimedOut.html';
}

-------

When I load the page and call StartSessionTimer(), I know it works
because the page redirects after ten minutes (the value of 60000).
However, in certain situations I need to be able to call back to the
server with AJAX and then have the timer reset - that's when I call
the RestartSessionTimer() function. When I do this, for some reason
the ten minute window does not get reset.

To troubleshoot, if I remove the second line in the
RestartSessionTimer() function the redirect is getting blocked (as
planned). However, when I put the second line back in, the page just
redirects as originally called - the SessionTimeout value is never
reset properly.

The code above looks good to me, but for some reason the
SessionTimeout var does not get reset in the RestartSessionTimer
function; it retains its original value?

How can I fix this?

Please help.

JP
 
T

Thomas 'PointedEars' Lahn

Hey guys I have some js code as follows...

-------

var SessionTimer;

function StartSessionTimer()
{
SessionTimer = setTimeout('RedirectToSessionTimedOutPage(),60000)

You have a syntax error in your code, the string literal is unfinished.

Also, the identifier should be `sessionTimer' (as it does not designate a
constructor) and the call should be `window.setTimeout(...)'.
}

function RestartSessionTimer()
{
clearTimeout(SessionTimer);
window.clearTimeout(sessionTimer);

StartSessionTimer();
}

function RedirectToSessionTimedOutPage()
{
window.location = '/SessionTimedOut.html';
}

This method really isn't necessary.

sessionTimer = window.setTimeout(
"window.location = '/SessionTimedOut.html';", 60000);

60'000 *milliseconds* are 60 seconds. That is *one* minute, _not_ ten
(that would be 600'000 ms).
[...]
To troubleshoot, if I remove the second line in the
RestartSessionTimer() function the redirect is getting blocked (as
planned). However, when I put the second line back in, the page just
redirects as originally called - the SessionTimeout value is never
reset properly.

Quite the contrary. It is *always* being reset, in a sense.
The code above looks good to me,

Given your description, the code you have posted is not the code you are
using, so any statement about its quality would be sheer speculation.
but for some reason the SessionTimeout var does not get reset in the
RestartSessionTimer function; it retains its original value?

All objects cease to exist (or at least are marked for garbage collection)
when the execution context they have been declared in is destroyed. That
happens for the global context, and so the Global Object of which global
variables are properties, whenever the document is being navigated away
from, such as caused by the assignment to `window.location'.

When the first `script' element in /SessionTimedOut.html is parsed, a
new global execution context is created.


PointedEars
 
J

joey.powell

Hey guys I have some js code as follows...

var SessionTimer;
function StartSessionTimer()
{
SessionTimer = setTimeout('RedirectToSessionTimedOutPage(),60000)

You have a syntax error in your code, the string literal is unfinished.

Also, the identifier should be `sessionTimer' (as it does not designate a
constructor) and the call should be `window.setTimeout(...)'.
function RestartSessionTimer()
{
clearTimeout(SessionTimer);
window.clearTimeout(sessionTimer);


function RedirectToSessionTimedOutPage()
{
window.location = '/SessionTimedOut.html';
}

This method really isn't necessary.

sessionTimer = window.setTimeout(
"window.location = '/SessionTimedOut.html';", 60000);
When I load the page and call StartSessionTimer(), I know it works
because the page redirects after ten minutes (the value of 60000).

60'000 *milliseconds* are 60 seconds. That is *one* minute, _not_ ten
(that would be 600'000 ms).
[...]
To troubleshoot, if I remove the second line in the
RestartSessionTimer() function the redirect is getting blocked (as
planned). However, when I put the second line back in, the page just
redirects as originally called - the SessionTimeout value is never
reset properly.

Quite the contrary. It is *always* being reset, in a sense.
The code above looks good to me,

Given your description, the code you have posted is not the code you are
using, so any statement about its quality would be sheer speculation.
but for some reason the SessionTimeout var does not get reset in the
RestartSessionTimer function; it retains its original value?

All objects cease to exist (or at least are marked for garbage collection)
when the execution context they have been declared in is destroyed. That
happens for the global context, and so the Global Object of which global
variables are properties, whenever the document is being navigated away
from, such as caused by the assignment to `window.location'.

When the first `script' element in /SessionTimedOut.html is parsed, a
new global execution context is created.

PointedEars

Thank you for your feedback.

The original code was on a different machine, so I typed it in instead
of copying in. That's why there were some errors in my posted code.

Now a couple of things...

(1) This time I actually copied and pasted the code in...so no syntax
errors:

<script type='text/javascript'>
var sessionTimer;
function StartSessionTimer()
{
sessionTimer =
window.setTimeout('RedirectToSessionTimedOutPage()', 600000);
}
function RestartSessionTimer()
{
clearTimeout(sessionTimer);
StartSessionTimer();
}
function RedirectToSessionTimedOutPage()
{
window.location = '/SessionTimedOut.html';
}
</script>


....and (2)...

I never leave/redirect the page when trying to reset the timeout
value; i just perform AJAX xml-http callbacks to the server. The idea
is, I set up the code above to handle redirecting me to the
'SessionTimedOut.html' page only when the value of 600000 milliseconds
(ten minutes) elapses. But when I do an AJAX style callback, I must
reset the value, so that redirect occurs ten minutes from *then*. I
have successfully set up the code to call the 'RestartSessionTimer'
function when the callback finishes, but the value of 600000
apparently is never reset; it always retains the original value.

An example: I load the page and then after about nine minutes (one
minute prior to expiration,) I do actions that make that make the AJAX
style callback and that call the 'RestartSessionTimer' function.
However the page still redirects only a minute later ... instead of in
*another* ten minutes as intended.

What am I doing wrong here?
 
S

stv_yip

Hey guys I have some js code as follows...

-------

var SessionTimer;

function StartSessionTimer()
{
SessionTimer = setTimeout('RedirectToSessionTimedOutPage(),60000)

}

function RestartSessionTimer()
{
clearTimeout(SessionTimer);
StartSessionTimer();

}

function RedirectToSessionTimedOutPage()
{
window.location = '/SessionTimedOut.html';

}

-------

When I load the page and call StartSessionTimer(), I know it works
because the page redirects after ten minutes (the value of 60000).
However, in certain situations I need to be able to call back to the
server with AJAX and then have the timer reset - that's when I call
the RestartSessionTimer() function. When I do this, for some reason
the ten minute window does not get reset.

To troubleshoot, if I remove the second line in the
RestartSessionTimer() function the redirect is getting blocked (as
planned). However, when I put the second line back in, the page just
redirects as originally called - the SessionTimeout value is never
reset properly.

The code above looks good to me, but for some reason the
SessionTimeout var does not get reset in the RestartSessionTimer
function; it retains its original value?

How can I fix this?

Please help.

JP

hi joey,

I had the same problem too. I understand your problem. Have you found
any solutions yet?
 
K

K2xL.com

I've had the same problem too! Using AJAX and clearTimer doesn't seem
to be working for me either.

-Danny
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top