js中setTimeout的使用

1、语法html


setTimeout(code,millisec)函数

code:是含有 JavaScript 语句的字符串。这个语句可能诸如 "alert('5 seconds!')",或者对函数的调用,诸如 alertMsg()。spa

millisec:指示从当前起多少毫秒后执行第一个参数。code

 

 

2、setTimeout(code,millisec)中code包含形参的用法htm

 

例如ip

 

var  msg= 'dfdsf' ;
 
setTimeout(alert(msg),1000); //alert(msg)会被当即执行

 

setTimeout(“alert(msg)”,1000);//系统报错

 

 

解决办法是使用匿名函数回调字符串

 

var msg='fewweeeee'; setTimeout(function(){alert(msg);},1000);



参考资料:  js setTimeout   http://www.studyofnet.com/news/916.htmlget

相关文章
相关标签/搜索