可能重复:
setTimeout的Internet Explorer中
我失去了一些东西在这里还是有Internet Explorer中的问题传递函数的参数,当一个setTimeout
调用同一个功能?
这将在Internet Explorer中运行下去:
function myFunction(myParam, tries){
if (typeof tries == "undefined"){
tries = 0;
}
tries++;
if (tries < 2){
setTimeout(myFunction, 50, myParam, tries);
}
}
myFunction("something");
有没有办法来解决这个问题?
http://fiddle.jshell.net/rH3gx/
的解释和解决方案是在MDN :
如果你需要一个参数传递给你的回调函数,但需要在Internet Explorer,它不支持发送额外的参数来工作(既不符合的setTimeout()或setInterval的())可以包括该IE特有的兼容性代码将只是在你的脚本的开头插入它能够在浏览器中的两个定时器HTML5标准参数通道功能。
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;
}
http://fiddle.jshell.net/rH3gx/2/
你需要用的功能,即不需要任何参数的函数:
function myFunction(myParam, tries){
if (typeof tries == "undefined"){
tries = 0;
}
tries++;
if (tries < 2){
setTimeout(function() {
myFunction(myParam, tries);
}, 50);
}
}
myFunction("something");
setTimeout(function(){myFunction(myParam, tries);}, 50);
http://fiddle.jshell.net/rH3gx/1/