window.setTimeout() 不能立即执行的情况

无论是window.setTimeout 还是window.setInterval,在使用函数名作为调用句柄时都不能带参数,而在许多场合必需要带参数,这就需要想方法解决.例如对于函数hello(_name),它用于针对用户名显示欢迎信息:
var userName="Tony";
//根据用户名显示欢迎信息
function hello(_name){
alert("hello,"+_name);
}
这时,如果企图使用以下语句来使hello函数延迟3 秒执行是不可行的:
window.setTimeout(hello(userName),3000);
这将使hello函数立即执行,并将返回值作为调用句柄传递给setTimeout 函数,其结果并不是程序需要的.而使用字符串形式可以达到想要的结果:
window.setTimeout("hello(userName)",3000);

posted @ 2009-09-21 17:48  依然神思者  阅读(483)  评论(0编辑  收藏  举报