标准浏览器与IE10都支持额外传参数,从第三个参数起,作为回调的参数传入。
var id = window.setTimeout(callback,10,1,2,4); function callback(i,j,k){ alert(i+j+k); clearTimeout(id); } //返回结果:7
ie6-ie9可以用以下代码模拟:(来自司徒正美的框架设计)
if(window.VBArray && !(document.documentMode>9)){ (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); } }) }
var m = 5; var id = window.setTimeout("callback(2,"+m+",3)",10); function callback(i,j,k){ alert(i+j+k); clearTimeout(id); } // 返回结果:10
setTimeout,setInterval回调传参,布布扣,bubuko.com
原文:http://my.oschina.net/wbo0801/blog/297705