By lonesomeday


2011-05-21 11:56:32 8 Comments

We all know that passing a string to setTimeout (or setInterval) is evil, because it is run in the global scope, has performance issues, is potentially insecure if you're injecting any parameters, etc. So doing this is definitely deprecated:

setTimeout('doSomething(someVar)', 10000);

in favour of this:

setTimeout(function() {
    doSomething(someVar);
}, 10000);

My question is: can there ever be a reason to do the former? Is it ever preferable? If it isn't, why is it even allowed?

The only scenario I've thought of is of wanting to use a function or variable that exists in the global scope but has been overridden in the local scope. That sounds to me like poor code design, however...

2 comments

@Jeremy J Starcher 2014-08-12 07:56:25

For those who are redirected here by the question of why is passing a function better than passing a string.

1: Passing a string fires up a compiler

Every time you have to evaluate a string, you fire up a full compiler. For each and every invocation where it is needed.

Not only is this slow, it destroys all of the JIT and browser speedups that are done.

2: Passing a string is MUCH more limited.

Because a string is run through a compiler, it isn't as cleanly bound to the local scope and variables.

While it isn't noticeable in situation like:

window.setInterval("doThing()");

In a more complex situation, the code is just cleaner:

window.setInterval("doThing(" + val1 + "," + val2 + ")");

vs

window.setInterval(function() {
  // You can put a debugging point here
  dothing(val1, val2);
});

3: DOM objects can't be passed via string

As Álvaro mentioned, DOM objects can not be passed via a string method.

// There is no way to do this via a string.
var el = document.getElementById("my-element");
window.setInterval(function() {
  dothing(el);
});

(Other objects may or may not be passable -- depending on if they can be serialized, but but in general it would be quite difficult.)

@Álvaro González 2014-08-12 08:08:16

Not to mention when you need to pass an object as argument (a DOM node, a date...).

@Jeremy J Starcher 2014-08-12 08:08:57

@ÁlvaroG.Vicario Ah yes! Excellent. I'll add that.

@Marcel Korpel 2011-05-21 12:08:07

You can always use global variables by accessing them as properties of the window object, like window.globalVar (though using globals is indeed not a good practice), so no, I don't think there is ever a good reason to use the deprecated syntax.

It is probably allowed for historical reasons: as Felix Kling mentioned, the original syntax did only allow to pass a string of code:

Introduced with JavaScript 1.0, Netscape 2.0. Passing a Function object reference was introduced with JavaScript 1.2, Netscape 4.0; supported by the MSHTML DOM since version 5.0. [source, my emphasis]

If browsers don't support the use of a string as first argument to setTimeout and setInterval anymore, there will be lots of code on the internet that doesn't function anymore.

@user166390 2011-12-11 07:52:15

Judging from the amount of examples on SO that use strings ... this is unfortunately very true.

@Álvaro González 2014-08-12 07:56:36

@472084 Yeah, and cancer should be cured.

Related Questions

Sponsored Content

85 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

37 Answered Questions

[SOLVED] Creating multiline strings in JavaScript

44 Answered Questions

[SOLVED] How to check empty/undefined/null string in JavaScript?

26 Answered Questions

[SOLVED] How can I pass a parameter to a setTimeout() callback?

27 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

43 Answered Questions

[SOLVED] Sort array of objects by string property value

59 Answered Questions

[SOLVED] How to replace all occurrences of a string?

3 Answered Questions

75 Answered Questions

[SOLVED] How can I convert a string to boolean in JavaScript?

  • 2008-11-05 00:13:08
  • Kevin
  • 1815860 View
  • 2338 Score
  • 75 Answer
  • Tags:   javascript

26 Answered Questions

[SOLVED] Can (a== 1 && a ==2 && a==3) ever evaluate to true?

Sponsored Content