setTimeOut传参数的时候的写法
2014-10-30 15:41
316 查看
无论是window.setTimeout还是window.setInterval,在使用函数名作为调用句柄时都不能带参数。
先看一个简单的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function show(){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
alert("Hello World");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
setTimeout(show,1000);
这段代码的效果是在1秒后显示hello world,但是如果改成
setTimeOut(show(),1000);
就会立即显示,达不到延时的效果了。但是如果加上引号就可以了。如:
setTimeOut("show()",1000);
就可以了。但是如果带上参数的话,还是不行比如:
setTimeOut("show(name)",1000)
这时就了一个比较的方法,就是再写个函数,该函数返回一个不带参数的函数,具体如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<scriptlanguage="javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function show(name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{alert("Hello
World:"+
name);}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function _show(name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
returnfunction()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
show(name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
setTimeout(_show(name),1000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
function
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
先看一个简单的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function show(){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
alert("Hello World");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
setTimeout(show,1000);
这段代码的效果是在1秒后显示hello world,但是如果改成
setTimeOut(show(),1000);
就会立即显示,达不到延时的效果了。但是如果加上引号就可以了。如:
setTimeOut("show()",1000);
就可以了。但是如果带上参数的话,还是不行比如:
setTimeOut("show(name)",1000)
这时就了一个比较的方法,就是再写个函数,该函数返回一个不带参数的函数,具体如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<scriptlanguage="javascript">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function show(name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{alert("Hello
World:"+
name);}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function _show(name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
returnfunction()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
show(name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
setTimeout(_show(name),1000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
function
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
相关文章推荐
- setTimeOut传参数的时候的写法
- iOS 发大招 otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法 && 编写通用类的时候关于可变参数的处理
- settimeout如何调用方法的时候,传递参数
- setTimeout 传递带参数的函数无效果
- springMVC 接受map参数的写法
- FFMPEG类库打开流媒体的方法(需要传参数的时候)
- 单击 < a > 的时候获取href并且让href增加一个参数
- IE中setTimeout和setInterval不支持回调函数参数的解决方法
- 创建fragment的适配器的时候,构造函数传入content参数
- mybatis参数中包含数组和集合时候怎么传参?
- 异步调用webservice方法的时候传的参数只能用大写而不能用小写
- MyBatis传入参数为集合、数组SQL写法
- sitemap文件写法详解及参数说明
- 调方法的时候 传的参数有值,进入方法内执行的时候 参数就变为空
- FFMPEG类库打开流媒体的方法(需要传参数的时候)
- 微信支付或者支付宝支付的时候参数ASCII码排序
- 模板函数多个类型参数时候怎么调用
- js中setTimeout方法重写,实现对象参数传递
- Android中inflate参数的写法:
- 视频参数相关的一些简短英文,防止要简短的时候找不到