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.

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

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.

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

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

Use an anonymous function instead:

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

Use an anonymous function:

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

How about this:

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

you can use closure:

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


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   How can I set Image source with base64

Similar Posts