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

相關文章
相關標籤/搜索