IE Parameters Get Undefined When Using Them In SetTimeout
Possible Duplicate: setTimeout Internet Explorer Am I missing something here or is there a problem in Internet Explorer when passing function parameters to a setTimeout calling
Solution 1:
The explanation and solution are in the MDN :
If you need to pass an argument to your callback function, but need it to work in Internet Explorer, which doesn't support sending additional parameters (neither with setTimeout() or setInterval()) you can include this IE-specific compatibility code which will enable the HTML5 standard parameters passage functionality in that browser for both timers just by inserting it at the beginning of your scripts.
if (document.all && !window.setTimeout.isPolyfill) {
var __nativeST__ = window.setTimeout;
window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
var aArgs = Array.prototype.slice.call(arguments, 2);
return __nativeST__(vCallback instanceof Function ? function () {
vCallback.apply(null, aArgs);
} : vCallback, nDelay);
};
window.setTimeout.isPolyfill = true;
}
if (document.all && !window.setInterval.isPolyfill) {
var __nativeSI__ = window.setInterval;
window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
var aArgs = Array.prototype.slice.call(arguments, 2);
return __nativeSI__(vCallback instanceof Function ? function () {
vCallback.apply(null, aArgs);
} : vCallback, nDelay);
};
window.setInterval.isPolyfill = true;
}
Solution 2:
http://fiddle.jshell.net/rH3gx/2/
You need to wrap the function in a function which requires no arguments:
function myFunction(myParam, tries){
if (typeof tries == "undefined"){
tries = 0;
}
tries++;
if (tries < 2){
setTimeout(function() {
myFunction(myParam, tries);
}, 50);
}
}
myFunction("something");
Solution 3:
setTimeout(function(){myFunction(myParam, tries);}, 50);
Post a Comment for "IE Parameters Get Undefined When Using Them In SetTimeout"