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:

Answer

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

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.

Answer

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

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

Use an anonymous function instead:

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

Use an anonymous function:

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

How about this:

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

you can use closure:

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.