<tr><td class="ys04"><span class="ys02">當前時間:</span></td><td colspan="2"><span class="showTime"></span><script>var t = null;t = setTimeout(time, 1000); //開啟定時器器,1s后執行timefunction time() {clearTimeout(t); //清除定時器dt = new Date();var y = dt.getFullYear(); //獲取當前年var mt = dt.getMonth() + 1; //獲取當前月份(0-11,0代表1月)var day = dt.getDate(); //獲取當前日(1-31)var h = dt.getHours(); //獲取當前小時(0 ~ 23)var m = dt.getMinutes(); //獲取當前分鐘(0 ~ 59)var s = dt.getSeconds(); //獲取當前秒數(0 ~ 59)document.querySelector(".showTime").innerHTML = y + "年" + mt + "月" + day + "-" + h + "時" + m + "分" + s + "秒";t = setTimeout(time, 1000); //再次定時器,循環運行}</script></td>
</tr>
setTimeout()?方法設置一個定時器,一旦定時器到期,就會執行一個函數或指定的代碼片段。
setTimeout()?函數語法:setTimeout(functionRef,?delay)
參數:
? ? ? ? 1)functionRef:當定時器到期后,將要執行的function
? ? ? ? 2)Delay:定時器在執行指定的函數或代碼之前應該等待的時間,單位是毫秒。如果省略該參數,則使用值 0,意味著“立即”執行。
結果:
web實現時間走秒