setTimeout(script,millisecond); 是從現在算起多少微秒後運行該代碼(只運行一次)
setInterval(script,millisecond); 是每隔多少微秒運行一次代碼
樣本:
1.執行一次
<span id ="time"></span> 秒 <script type ="text/javascript"> var i = 1 ; var txt = document.getElementById( " time " ); function addT(i) { txt.innerHTML = i; } setTimeout(" addT(i++) " , 1000 ); </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
2.複製執行
<span id ="time"> 3 </span> 秒 <script type ="text/javascript"> var i = 1 ; var txt = document.getElementById( " time " ); function addT(i) { txt.innerHTML = i; } setInterval( " addT(i++) " , 1000 ); </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
setTimeout也可以做成重複執行
<span id ="time"></span> 秒 <script type ="text/javascript"> var i = 1 ; var txt = document.getElementById( " time " ); function addT(i) { txt.innerHTML = i; setTimeout( " addT(i++) " , 1000 ); } addT(i); </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]