Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout Internet Explorer

Tags:

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.

like image 969
clamp Avatar asked Mar 05 '12 14:03

clamp


People also ask

What is setTimeout ()?

setTimeout() The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.

What can I use instead of setTimeout?

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.

Is setTimeout deprecated?

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

How do I use setTimeout?

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


2 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); 
like image 198
Rob W Avatar answered Nov 03 '22 05:11

Rob W


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.

like image 29
Joseph Silber Avatar answered Nov 03 '22 05:11

Joseph Silber