Inquisitive
Inquisitive

Reputation: 7836

Why is setTimeout("otherFunction()",2000) wrong?

I am a newbie to java script and currently reading John Resig's Pro javascript techniques . While explaining closure he refers to calls like setTimeout("otherFunction()",2000) as instances where new JS developers have problems . I could not understand why this is a problem ? Can some one explain please ?In this http://www.w3schools.com/js/js_timing.asp I am seeing a call like var t=setTimeout("alertMsg()",3000); which looks similar to me .

Upvotes: 1

Views: 705

Answers (5)

Pacerier
Pacerier

Reputation: 89613

Anything enclosed with "" is a string so a JavaScript interpreter will generally need to parse the string.

Parsing the string is unnecessary even if it works.

If we simply use

setTimeout(alertMsg,3000);,

the interpreter is not required to do any additional (unnecessary) work, resulting in better code.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

The recommended approach is to use the following:

setTimeout(otherFunction, 2000);

or a closure:

setTimeout(function() {
    otherFunction();
}, 2000);

Do not use the overload which takes a string as first parameter because the javascript interpreter will need to parse this string into javascript code.

And yeah, the site you have linked to http://www.w3schools.com is probably one of the worst sites out there to learn programming. It shows exactly what you shouldn't do.

Upvotes: 5

nnnnnn
nnnnnn

Reputation: 150020

It's not "wrong", it's just not necessarily "right", and it's certainly not recommended.

The setTimeout() function's first parameter can be a string or a function reference / function expression.

If you pass a string it will be slower because effectively you are doing an eval() which is not recommended. More important than speed though is that the scope in which the code in the string executes may not be what you are expecting (and may not be the same in different browsers).

By passing a function reference / function expression instead these problems can be avoided.

The "right" syntax for your example is:

setTimeout(otherFunction, 2000);

Note there are no parentheses after otherFunction - if there were it would call otherFunction() immediately and pass the return value from that function to setTimeout().

If you need to pass parameters to your function you can wrap it in an anonymous function:

setTimeout(function() {
    otherFunction(param1, param2);
}, 2000);

That may seem kind of clunky compared to setTimeout("otherFunction(param1,param2)", 2000) but again it avoids issues with scope of where otherFunction, param1 and param2 are defined.

Upvotes: 6

xdazz
xdazz

Reputation: 160833

Instead of setTimeout("otherFunction()",2000), pass the function directly by doing setTimeout(otherFunction,2000) is much better. The previous way has to do eval the string "otherFunction()".

Upvotes: 0

KooiInc
KooiInc

Reputation: 122906

Because it has to eval otherFunction() (and hence spawn a new instance of the interpreter) every time. If you provide a reference to the function, setTimeout can execute it without the necessity to spawn a new interpreter.

So use:

setTimeout(otherFunction,2000);

Upvotes: 1

Related Questions