Pass parameter to setTimeout

setTimeout is a very general function in JavaScript which delay an execuation of a portion of code/ function call. And I used it in ActionScript as well so I was deeply get used to write the function in this format:

var param = “Hello”;

var timer = setTimeout(function(inParam){

//coding

}, 1000,  param);

Where ‘param’ is the parameter I wish to pass into the function with variable ‘inParam’ for further processing.

I didn’t realize until today I run it in IE, I got the hasty yellow exclamation mark on the left bottom status bar, saying the variable ‘inParam’ is undefined. Eventually I googled it and found that IE did a great job that she stand out from the browser keep not supporting the above syntax for passing parameter to setTimeout(). Or maybe it is too kind for other browsers to provide the above method for developer to write rapid parameter passing code.

The work around for IE to allow passing parameter to setTimeout function is:

var param = “Hello”;

var fnc = function(inParam){

//coding with the use of inParam

}

var timer = setTimeout(function(){

fnc(param);

}, 1000);

Or it is the time for me to use this complicate syntax to fit for all browsers~

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *

Please Answer * Time limit is exhausted. Please reload CAPTCHA.