/***********************************
*Author:Java619
*Time:2006-08
************************************/
也许你过去在setTimeout中传参数一直是这样
setTimeout("pass(" + argu + ")",1000)
这样只能传字符串,对传递object就无能为力了,需要大费文章.然而别忘了,第一个参数还可以是function
下面举个实例:
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
TITLE
>
向setTimeout传递函数参数
</
TITLE
>
</
HEAD
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<
script
type
="text/javascript"
>
...
var _st = window.setTimeout;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
window.setTimeout = function(fRef, mDelay) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(typeof fRef == 'function')...{
var argu = Array.prototype.slice.call(arguments,2);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
var f = (function()...{ fRef.apply(null, argu); });
return _st(f, mDelay);
}
return _st(fRef,mDelay);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
function hello(x,y)...{
alert(x+" "+y);
}
window.setTimeout(hello,1000,'Hello','Ceun');
</
script
>
<
BODY
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
</
BODY
>
</
HTML
>
现在向setTimeout中传递带参数的函数是不是方便多了,其实就是重载了下window.setTimeout
用apply去回调前面的函数。