首页 > 技术文章 > setTimeout调用带参数的函数的方法

james641 2016-10-20 11:21 原文

function test(s)
{
    alert(s);
}
window.setTimeout(function(){test('str');},1000);
这样就可以了...
为什么是这样呢.
因为setTimeout要求的第一个参数是函数...比如setTimeout(a_fun,1000)这样是可以的,a_fun是一个函数.
但是setTimeout(a_fun(),1000);这样就不行了.因为这里a_fun()其实是函数的返回值了...这样讲应该能明白了.
另外,不推荐网上有人用的方法setTimeout('test(1)',1000);这样的形式,因为这个有很多情况下是不实用的.
如果实在要用这种方法,只能这样:setTimeout("test('"+param+"')",100);这种方式。

 

转自 http://blog.csdn.net/sunbirdhan/article/details/41048581

推荐阅读