23

I have the following javascript in MSIE:

setTimeout(myFunction, 1000, param );

this seems to work in all browsers except internet explorer. the param just doesnt get forwarded to the function. looking at the debugger, it is undefined.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
clamp
  • 33,000
  • 75
  • 203
  • 299

7 Answers7

42

param in Internet explorer specifies whether the code in myFunction is JScript, JavaScript or VBscript See also: MSDN. It does not behave like other browsers.

The following will work:

setTimeout(function() {
    myFunction(param);
}, 1000);

The previous line does not exactly mimic setTimeout in Firefox etc. To pass a variable, unaffected by a later update to the param variable, use:

setTimeout( (function(param) {
    return function() {
        myFunction(param);
    };
})(param) , 1000);
Huangism
  • 16,278
  • 7
  • 48
  • 74
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • @clamp The `param` at the bottom is the original `param` variable. `param` at the top is a local variable for the self-invoking function. `param` at the middle resolves to `param` at the top. – Rob W Mar 05 '12 at 16:59
  • thank you! everything seems to work now in all browsers, just some post processing tools like jshint and google closure compiler complain that this is invalid syntax. but i guess, this should be another question then. – clamp Mar 05 '12 at 17:02
  • I don't get any warnings/errors with the snippet alone in the [Closure Compiler](http://closure-compiler.appspot.com/home) or [JSHint](http://www.jshint.com/). – Rob W Mar 05 '12 at 18:01
  • i have to mention that the parameter is an array like this: [a,b] – clamp Mar 06 '12 at 09:09
  • @clamp That's still not sufficient. If you really want to solve this (interesting?) problem, don't hesitate to post a new question, including the relevant code to demonstrate your case. – Rob W Mar 06 '12 at 15:21
4

Internet Explorer does not allow you to pass parameters like that. You'll have to do it explicitly from the callback function:

setTimeout(function(){
    myFunction(param);
}, 1000);

Quote from MDN:

Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
3

Take a look at http://www.makemineatriple.com/2007/10/passing-parameters-to-a-function-called-with-settimeout

Looks like you'll need something like this:

setTimeout(function(){ myFunction(param) }, 1000);
Tim M.
  • 53,671
  • 14
  • 120
  • 163
mrmonroe
  • 325
  • 1
  • 10
1

That isn't a parameter. Apparently, that last argument is denoting the scripting language.

Use an anonymous function instead:

setTimeout(function() {
  myFunction(param);
}, 1000);
Blender
  • 289,723
  • 53
  • 439
  • 496
1

Use an anonymous function:

setTimeout(function() { myFunction(param) }, 1000);
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

How about this:

setTimeout(function(){
    myFunction(param);
}, 1000);
papaiatis
  • 4,231
  • 4
  • 26
  • 38
1

you can use closure:

setTimeout(function(){myFunction(param)}, 1000);
Vikram
  • 8,235
  • 33
  • 47