<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
function showClock(){
var today=new Date();
var display=document.getElementById('display');
display.innerHTML=today.toLocaleString();
timer=window.setTimeout("showClock()",1000);
}
</script>
</head>
<body>
<p>
<input type="button" name="button" value="開始" onClick="showClock()">
<input type="button" name="button1" value="中止" onClick="window.clearTimeout(timer)">
</p>
<div id="display"></div>
</body>
</html>javascript
收穫:
1.用Date()獲取當前時間。
2.用setTimeout("timer()",1000)方法表示在指定時間後調用某一方法,須要的參數爲兩個:須要調用的方法名和指定的時間,這裏使用的是毫秒單位。
3.setTimeout()在指定的毫秒數後調用函數或計算表達式。
4.clearTimeout()取消由setTimeout()方法設置的定時器。html
學習了怎麼獲取當前時間並讓它顯示出來,還知道如何讓時間中止,如何在繼續。
java