[thelist] JS Functions: calling themselves et al

Scott Brady evolt at scottbrady.net
Wed Nov 13 14:41:00 CST 2002


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

Scott





More information about the thelist mailing list