It appears the ability has been added to some browsers pass parameters to setTimeout:
syntax: setTimeout (function (p1,p2) {},1000,p1,p2);
(add as many params as you want)
If you want to ensure it works everywhere, you can use the attached code.
Note: If you want to set a timeout immediately after installing it, it's best to use the callback parameter and do it in there
for example
installSwizzledTimeout(function(param1,param2){
setTimeout(myFunc,200,param1,param2);},param1,param2);
}
This is because it uses a trick to detect if it is needed, by setting a very short timeout and counting the parameters.
window.swizzledSetTimeout = function (fn, ms) {
if (arguments.length === 2) {
//console.log("Bypassing swizzledSetTimeout");
return window.originalSetTimeout(fn, ms);
} else {
var args = [];
for (i = 2; i < arguments.length; i++) {
args.push(arguments[i])
};
//console.log("Setting swizzledSetTimeout for function (",args,") {...} in ",ms," msec");
var retval = window.originalSetTimeout(function () {
//console.log("Invoking swizzledSetTimeout for function (",args,") {...}");
fn.apply(null, args);
}, ms);
return retval;
}
}
function installSwizzledTimeout(cb) {
var args = [];
for (i = 1; i < arguments.length; i++) {
args.push(arguments[i])
};
setTimeout(function (arg) {
//console.log("arguments.length:",arguments.length,window.setTimeout.toString());
if (arguments.length == 0) {
function doInstall() {
//console.log("Installing new setTimeout");
window.originalSetTimeout = window.setTimeout;
window.setTimeout = function setTimeout() {
return window.swizzledSetTimeout.apply(null, arguments);
};
if (cb) {
cb.apply(null, args);
};
}
if (window.setTimeout.toString().indexOf("swizzledSetTimeout") < 0) {
doInstall();
}
} else {
//console.log("existing set time supports arguments ");
if (cb) {
cb.apply(null, args);
};
}
}, 0, 1, 2, 3, 4);
}