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.
setTimeout() The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.
We all know that passing a string to setTimeout (or setInterval ) is evil, because it is run in the global scope, has performance issues, is potentially insecure if you're injecting any parameters, etc. So doing this is definitely deprecated: setTimeout('doSomething(someVar)', 10000);
The setTimeout() method executes a block of code after the specified time. The method executes the code only once. The commonly used syntax of JavaScript setTimeout is: setTimeout(function, milliseconds);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With