setTimeout() 方法用於在指定的毫秒數後調用函式或計算表達式。
基本介紹
- 外文名:setTimeout()
- 語法:setTimeout(code,millisec)
- 用途:調用函式或計算表達式
- 參數:millisec、code
語法
參數
<html><head><scripttype="text/javascript">functiontimedMsg(){vart=setTimeout("alert('5seconds!')",5000)}</script></head><body><form><inputtype="button"value="Displaytimedalertbox!"onClick="timedMsg()"></form><p>Clickonthebuttonabove.Analertboxwillbedisplayedafter5seconds.</p></body></html>
functionclockon(bgclock){varnow=newDate();varyear=now.getFullYear();varmonth=now.getMonth();vardate=now.getDate();varday=now.getDay();varhour=now.getHours();varminu=now.getMinutes();varsec=now.getSeconds();varweek;month=month+1;if(month<10)month="0"+month;if(date<10)date="0"+date;if(hour<10)hour="0"+hour;if(minu<10)minu="0"+minu;if(sec<10)sec="0"+sec;/*vararr_week=newArray("星期日","星期一","星期二","星期三","星期四","星期五","星期六");week=arr_week[day];*/switch(day){case1:week="星期一";break;case2:week="星期二";break;case3:week="星期三";break;case4:week="星期四";break;case5:week="星期五";break;case6:week="星期六";break;default:week="星期日";break;}vartime="";time=year+"年"+month+"月"+date+"日"+week+""+hour+":"+minu+":"+sec;if(document.all){bgclock.innerHTML="系統公告:["+time+"]"}vartimer=setTimeout("clockon(bgclock)",200);}
執行
var msg='1shaspassed!';
setTimeout(alert(msg),1000);//alert(msg)會被立即執行
setTimeout(“alert(msg)”,1000);//系統報錯msgisnotdefined(chrome)
var msg='1shaspassed!';setTimeout(function(){alert(msg);},1000);