HighDots Forums  

setTimeout - clearTimeout - Why Doesn't This Work?

Javascript JavaScript language (comp.lang.javascript)


Discuss setTimeout - clearTimeout - Why Doesn't This Work? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
joey.powell@topscene.com
 
Posts: n/a

Default setTimeout - clearTimeout - Why Doesn't This Work? - 10-25-2007 , 05:38 PM






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


Reply With Quote
  #2  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: setTimeout - clearTimeout - Why Doesn't This Work? - 10-25-2007 , 06:21 PM






joey.powell (AT) topscene (DOT) com wrote:
Quote:
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(...)'.

Quote:
}

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

Quote:
StartSessionTimer();
}

function RedirectToSessionTimedOutPage()
{
window.location = '/SessionTimedOut.html';
}
This method really isn't necessary.

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

Quote:
-------

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

Quote:
[...]
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.

Quote:
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.

Quote:
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
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>


Reply With Quote
  #3  
Old   
joey.powell@topscene.com
 
Posts: n/a

Default Re: setTimeout - clearTimeout - Why Doesn't This Work? - 10-31-2007 , 01:42 PM



On Oct 25, 5:21 pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:
Quote:
joey.pow... (AT) topscene (DOT) com wrote:
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);

-------

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
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm
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?



Reply With Quote
  #4  
Old   
stv_yip@yahoo.com
 
Posts: n/a

Default Re: setTimeout - clearTimeout - Why Doesn't This Work? - 12-05-2007 , 10:25 PM



On Oct 26, 5:38 am, joey.pow... (AT) topscene (DOT) com wrote:
Quote:
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?


Reply With Quote
  #5  
Old   
K2xL.com@gmail.com
 
Posts: n/a

Default Re: setTimeout - clearTimeout - Why Doesn't This Work? - 12-14-2007 , 06:00 PM



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

-Danny


On Dec 5, 10:25 pm, "stv_... (AT) yahoo (DOT) com" <stv_... (AT) yahoo (DOT) com> wrote:
Quote:
On Oct 26, 5:38 am, joey.pow... (AT) topscene (DOT) com wrote:



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?


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.