claracruz
asked on
window.clearTimeout and setTimeout
Hi Experts,
I have a function I'm using in my ajax.js file that does something like the following;-
var t
function alertUser(cursor, type)
{
//do ajax get request stuff
if (cursor == 1)
{
t = setTimeout("alertUser("+cu rsor+", "+type+")", 5000);
}
else if (cursor == 2)
{
window.clearTimeout(t);
}
}
I am calling the alertUser(cursor, type) function depending on activities performed by a user. For example when the page first intitalises, the following call is made;-
<script language="javascript">
window.onload = function() {
alertUser(1, 'false');
};
</script>
When a user performs a certain function on the page, I call the following;-
alertUser(2, 'true');
The trouble is that even when cursor = 2 that is when the following code is activated;-
window.clearTimeout(t);
The initial t = setTimeout("alertUser("+cu rsor+", "+type+")", 5000); is still running.
It would appear that window.clearTimeout(t); does not work...
Is there something I'm doing wrong or is this not doable with an ajax page that is called at different times for execution.
Many thanks,
C
I have a function I'm using in my ajax.js file that does something like the following;-
var t
function alertUser(cursor, type)
{
//do ajax get request stuff
if (cursor == 1)
{
t = setTimeout("alertUser("+cu
}
else if (cursor == 2)
{
window.clearTimeout(t);
}
}
I am calling the alertUser(cursor, type) function depending on activities performed by a user. For example when the page first intitalises, the following call is made;-
<script language="javascript">
window.onload = function() {
alertUser(1, 'false');
};
</script>
When a user performs a certain function on the page, I call the following;-
alertUser(2, 'true');
The trouble is that even when cursor = 2 that is when the following code is activated;-
window.clearTimeout(t);
The initial t = setTimeout("alertUser("+cu
It would appear that window.clearTimeout(t); does not work...
Is there something I'm doing wrong or is this not doable with an ajax page that is called at different times for execution.
Many thanks,
C
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Ah... here in lies the clue that helped me resolve the problem (If alertUser() is called with cursor==2,it WILL clear the timeout)...
Not sure if what I got from that is what you meant, but essentially, on testing I found that each time I made the javascript call with alertUser(1,' type') a new timer was started as per my conditional statement.
So I needed a way to call the timer just once which was at page load. So I editied the window.onload event to call thus;-
alertUser(0,' type')
and then set the conditional statement to start timeout only when cursor = 0. So the settimeout is then only called once rather than again and again and again.
And the rest tell me if I should stop the timer or start it or do nothing...
So I adjusted the code to read thus;-
if (cursor == 0)
{
t = setInterval("alertUser(1)" , 5000);
type = 'false';
}
else if (cursor == 1)
{
type = 'false';
}
else if (cursor == 2)
{
type = 'true';
}
else if (cursor == 3)
{
type = 'false';
t = 1
}
Additionally, it became unnecessary to stop the timer, as I could just check what returnValue was and either 'do something' or do nothing' The timer is required for the length of time the application is running, so there's no real need to keep starting and stopping the timer. Just prevent the relevant event where the user's status does not meet criteria.
Coolio...!!!
Not sure if what I got from that is what you meant, but essentially, on testing I found that each time I made the javascript call with alertUser(1,' type') a new timer was started as per my conditional statement.
So I needed a way to call the timer just once which was at page load. So I editied the window.onload event to call thus;-
alertUser(0,' type')
and then set the conditional statement to start timeout only when cursor = 0. So the settimeout is then only called once rather than again and again and again.
And the rest tell me if I should stop the timer or start it or do nothing...
So I adjusted the code to read thus;-
if (cursor == 0)
{
t = setInterval("alertUser(1)"
type = 'false';
}
else if (cursor == 1)
{
type = 'false';
}
else if (cursor == 2)
{
type = 'true';
}
else if (cursor == 3)
{
type = 'false';
t = 1
}
Additionally, it became unnecessary to stop the timer, as I could just check what returnValue was and either 'do something' or do nothing' The timer is required for the length of time the application is running, so there's no real need to keep starting and stopping the timer. Just prevent the relevant event where the user's status does not meet criteria.
Coolio...!!!
ASKER
Nothing happen with that, I guess (t) has no value.. it sort of empties ones the page has run on an ajax call..
Any ideas??!!!!