...

View Full Version : window.setTimeout kitty cat



Beck
02-21-2003, 07:02 AM
I think this is the first time I've ever posted a script out of morbid curiosity. Usually, the entire galaxy is at stake (and, of course, I blame you guys if I don't get it debugged ;) ). This time, I just thought the question was weird enough to be worthy.

Question: how do you test if a setTimeout has been cleared. For instance:

var fred;

function Joe () {
fred = window.setTimeout("Joe();",2000);
}

function bob () {
// whatever, but I can see fred!
}


somewhere in the html:
<input type=button onClick="bob();">

on click, can bob use fred to tell if joe is about to fire (that is, within 2000 milliseconds)?

Beck
02-21-2003, 10:47 AM
In the spirit of making this even more interesting (and bringing this particular posting back to the top of the list of threads in the forum for a few more hours), I have an additional thought to the above...

is there a way to redirect a window.setTimeout in the middle of the wait. For instance...

var timer = window.setTimeout("bob();",5000);

somehow set the timer to point to joe() instead, but in the same five seconds. Can it be done..?

I await thy answer.... :thumbsup:

RoyW
02-21-2003, 03:34 PM
That timeout is never "cleared" because the same function is called recursively. If you add a third function you can do


var fred = -1;

function joe()
{
fred = setTimeout("fred=0;billy();",2000);
}

function billy()
{
alert("timer fired");
}

function bob()
{
if(fred == -1)
alert("Timer was never started");
else if(fred == 0)
alert("Timer completed");
else
alert("Timer running - but I don't know how long is left");
}

However, you could probably create a new Date() that is set when the timer starts and compare it to a new Date() when bob is called to check the time remaining.
------------------------------------------------------------
Not sure what "in the same five seconds" means but I am assuming that if you start the timer - then click a button 2 seconds later you want joe() to execute 3 seconds after that (ie 5 seconds from when the timer started.


bob=function()
{
alert("Bob");
}

joe=function()
{
alert("joe");
}

doIt = bob;

var timerId = null;
doBob = function()
{
doIt = bob;

if(timerId != null)
timerId = setTimeout("doIt();",5000);
}
doJoe = function()
{
doIt = joe;

if(timerId != null)
timerId = setTimeout("doIt();",5000);
}


Here, if you call doBob() and then 3 seconds later call doJoe() then the script will alert "joe" 2 seconds later which is 5 seconds after doBob() was initially called.

This is done by using the var "doIt" which ends up being a pointer to the function to be executed.
e.g.


doIt = bob;
doIt();

will alert "bob"


doIt = joe;
doIt();

will alert "joe"

missing-score
02-21-2003, 08:56 PM
I think that the reason why you cant see fred is because It is within another function. Variables within a function are not set outside of that function. You would need to use a return statement.

Beck
02-22-2003, 01:54 AM
fred is global, he can be seen from anywhere. No need return statements, he belongs to all, although I think if you create another fred variable inside some function, that particular instance of fred belongs to that function.

RoyW I like your code style. Thanks for the ideas. Not sure when I might use them, but I didn't think of that first part, where you assign values to fred other than setTimeout return values.

Koo'

missing-score
02-22-2003, 11:14 AM
I missed the fred before the functions. So you are right beck.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum