When using setTimeout or setInterval in IE, there is no way to pass any data to the method you want to execute.
You can obviously create lots of global variables, but this is a very messy solution, and not object oriented.
Similarly, if you are using an API that makes an A-Synchronous call and you can set a callback handler, you may well want to pass extra data to the handler, and the API may not let you.
To get round this you can use a delegate function. You basically tell another function what to execute, what to pass it and the scope to execute it in.
NOTE : This method uses jQuery for browser detection. However you can add whichever browser detection you like and replace the $.browser.mozilla check in the method.
/**
* @param scope Object : The scope in which to execute the delegated function.
* @param func Function : The function to execute
* @param data Object or Array : The data to pass to the function. If the function is also passed arguments, the data is appended to the arguments list. If the data is an Array, each item is appended as a new argument.
* @param isTimeout Boolean : Indicates if the delegate is being executed as part of timeout/interval method or not. This is required for Mozilla/Gecko based browsers when you are passing in extra arguments. This is not needed if you are not passing extra data in.
*/
function delegate(scope, func, data, isTimeout)
{
return function()
{
var args = Array.prototype.slice.apply(arguments).concat(data);
//Mozilla/Gecko passes a extra arg to indicate the "lateness" of the interval
//this needs to be removed otherwise your handler receives more arguments than you expected.
//NOTE : This uses jQuery for browser detection, you can add whatever browser detection you like and replace the below.
if (isTimeout && $.browser.mozilla)
args.shift();
func.apply(scope, args);
}
}