虽然除了IE不支持第三个参数外,但Firefox和Safari/Chrome/Opera之间还是有区别的
代码如下:
setTimeout(function(){
alert(arguments.length);
}, 2000, 1,2);
传了两个参数1,2给回调函数,然后alert出实参的长度
Firefox : 3
Safari/Chrome/Opera : 2
奇怪吧,明明传的是两个参数,但Firefox中弹出的却是3。如果输出第三个参数会发现它是一个数字,有时还是负数。
关:
http://www.w3.org/TR/Window/
https://developer.mozilla.org/en/DOM/window.setTimeout
http://msdn.microsoft.com/en-us/library/ms536753%28v=vs.85%29.aspx
//解决IE下setTimeout传参数的bug
代码如下:
//解决IE下setTimeout传参数的bug
if(!+[1,]) {
(function(overrideFun){
window.setTimeout = overrideFun(window.setTimeout);
window.setInterval = overrideFun(window.setInterval);
})(
function(originalFun){
return function(code, delay){
var args = [].slice.call(arguments, 2);
return originalFun(
function(){
if (typeof code == 'string') {
eval(code);
}
else {
code.apply(this, args);
}
},
delay
)
}
}
);
}