Sunday, March 7, 2010

Assigning SetTimeout/SetInterval Functions with Argument(s)

If Javascipt’s SetTimeout()/SetInterval() is assigned with a function that contains argument, it won’t run. After trials and errors, here’s the way that works well with latest version of IE, Firefox, Safari and Chrome:

setInterval(function(){function_name(argument)}, 1000);

Noted those highlighted in red is a function to call another function. I have tried using eval(), it works well in Firefox but it won’t in IE. This trick is to make sure it works with all type of browsers. Using common sense, one may resort to “setInterval(function_name(argument), 1000);”, but it won’t run.

Update 2012 January 14: Please don't put quotation marks around the function.

No comments:

Post a Comment