clamp
clamp

Reputation: 34006

setTimeout Internet Explorer

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.

Upvotes: 23

Views: 37926

Answers (7)

Rob W
Rob W

Reputation: 348992

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);

Upvotes: 42

mrmonroe
mrmonroe

Reputation: 325

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);

Upvotes: 3

Vikram
Vikram

Reputation: 8333

you can use closure:

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

Upvotes: 1

papaiatis
papaiatis

Reputation: 4291

How about this:

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

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114367

Use an anonymous function:

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

Upvotes: 1

Blender
Blender

Reputation: 298166

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

Use an anonymous function instead:

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

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219938

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.

Upvotes: 4

Related Questions