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.

Answers


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

Need Your Help

Angular4 placeholder for a select

angular select

I'm trying to add a placeholder to a select on Angular 4 but no way to make it works,

Generate a random number with max, min and mean(average) in Java

java random max average min

I need to generate random numbers with following properties.