That code will certainly do the trick. Because your anonymous function
expression simply calls crossfade(), you're essentially doing the same as
this:

    cInterval = window.setInterval( crossfade, 5000 );

Well, there is a subtle difference. Your code below doesn't evaluate the
name "crossfade" until your function expression is called. The code above
evaluates the name (but doesn't call the function) at the time the
setInterval call is made. But in your code the net effect is the same, since
the crossfade function exists at the time this code is run.

When you got the missing quotes error, did the error message actually read
like this?

    Error: useless setTimeout call (missing quotes around argument?)

That's because you tried to write this code:

    cInterval = window.setInterval( crossfade(), 5000 );

Here's what that code does, in this order:

1) Calls the crossfade() function immediately.

2) Passes the *return value* of the crossfade() function to setInterval.

But crossfade() presumably does not return any value - which is to say that
it returns the 'undefined' value, so it's the same as if you'd coded:

    cInterval = window.setInterval( undefined, 5000 );

And of course that is not what you intended.

Note the difference between this code:

    cInterval = window.setInterval( crossfade(), 5000 );

and this working code:

    cInterval = window.setInterval( crossfade, 5000 );

When you use a function name without (), you get a reference to the
function, which you can call later by using that reference (as setInterval
does). When you use () after a function name, you call the function
immediately and get its return value.

Two other notes...

You can leave out the 'window.':

    cInterval = setInterval( crossfade, 5000 );

And, I assume you have a 'var cInterval;' somewhere inside your outer
function? I get nervous when I see a variable assignment without a 'var',
unless I know there's a 'var' somewhere in the code for it. Otherwise you'd
be unintentionally creating a global variable.

-Mike

On Tue, Nov 24, 2009 at 11:33 AM, Magnificent <
imightbewrongbutidontthin...@gmail.com> wrote:

> Hi Mike,
>
> Thanks much for your reply.  I dropped in an anonymous function and
> that fixed it:
>
> cInterval = window.setInterval(function() { crossfade(); }, 5000);
>
> I tried calling crossfade() without quotes, but FF gave me a missing
> quotes error.
>

Reply via email to