[thelist] JS Functions: calling themselves et al

Tom Dell'Aringa pixelmech at yahoo.com
Wed Nov 13 14:45:01 CST 2002


--- Scott Brady <evolt at scottbrady.net> wrote:
> >Question:
> >
> >Note the foo() function which is empty. The counting() function
> >essentially wants to call itself and continue counting down, but
> if I
> >set the line
> >
> >else foo = setTimeout("counting()",1000);
> >
> >to be
> >else counting = setTimeout("counting()",1000);
> >
> >I get an error. It seems as though it can't call itself. I was
> >wondering if anyone could explain this and if there is a way
> around
> >it. I guess its a recursive thing I am after.
>
> I don't think the problem is that the function is calling itself.
> I think it's that you're trying to use a variable name with the
> same name as the function "counting".
>
> In your "else" statement, you're not really using the variable
> "foo" or "counting" in that statement. (If you leave it "else foo =
> ..." and remove the foo() function, the page still works.
>
> In fact, you don't even need to set the result of the setTimeout()
> function to a variable. This also works:
>
> else setTimeout("counting()",1000); //can't call itself
>
>
> Someone else will probably have a clearer explanation, but this is
> clear in my head and I hope it helps.

Yes that helps, using simply the else with the setTImeout works fine.
So there you have a recursive function. I hacked this from other code
so not sure why the author had it that way.

Now...to get this to work multiple times on the page...

Tom

=====
var me = tom.pixelmech.webDeveloper();

http://www.pixelmech.com/
http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

__________________________________________________
Do you Yahoo!?
U2 on LAUNCH - Exclusive greatest hits videos
http://launch.yahoo.com/u2



More information about the thelist mailing list