views:

42

answers:

3
var my_new_function = function(){
----
};
window.setTimeout(my_new_function, 1600);

the above works properly without any errors.

when i use:

window.setTimeout("my_new_function()", 1600);

it's working properly, but firebug is showing error :

my_new_function is not defined

in some articles about setTimeout, i found calling functions like in the 1st method, and in some other articles, i saw the other method.

which is more correct? and why is firebug showing such an error?

+1  A: 

It doesn't matter which you use. If you pass a string, it will be turned into a function when the timer is fired. The first method looks a lot cleaner to me, though.

Note that passing a string runs it in window scope, so functions and other local variables may not be present and will fail.

Delan Azabani
i also thought both were the same, until firebug showed error when i used the 2nd method. any ideas why firebug showed error as 'the function is not defined'?
Anish
Weird... your example works fine for me.
Delan Azabani
Run this and my post explains why it fails:(function(){var my_new_function = function(){ alert("a");};window.setTimeout("my_new_function()", 1600);})();
epascarello
@Delan Azabani: the function was in local scope. i guess that was the issue as explained by epascarello
Anish
+3  A: 

When you call the function with

window.setTimeout(my_new_function, 1600);

You are setting the reference to the function.

The function in your second setTimeout example

window.setTimeout("my_new_function()", 1600);

needs to be evaluated when it is executed. When it is evaluated it is being executed in global scope. So if the function is in local scope the browser will not find it. [Sounds like this is your issue]

Seasoned developers will not recommend using strings in setTimeout since they need to be evaluated each time. All that means is it takes longer to execute.

Another option to call setTimeout is

window.setTimeout( function(){ my_new_function(); }, 1600);
epascarello
Using a string takes longer to execute, and it makes it much more annoying to pass arguments into these functions. There's never a time where using a string is a better alternative.
Gareth
@epascarello: When it is evaluated it is being executed in global scope. So if the function is in local scope the browser will not find it. Yup, the function was in local scope. i guess that was the reason.
Anish
@epascarello: "Seasoned developers will not recommend using strings in setTimeout since they need to be evaluated each time." Can you explain a little more?
Anish
@Anish, Gareth explained it above in the comments. Takes longer and harder to pass arguments. You have to make sure quotes are correct or escape them. You can only pass string, numbers, not objects.
epascarello
A: 
window.setTimeout("my_new_function()", 1600);

doesn't work because it is equivalent to:

window.setTimeout("window.my_new_function()", 1600);

and there's no such function defined in the window scope. You are declaring it in a local variable scope.

Darin Dimitrov