最近学习了JavaScript,发现JavaScript真是强大,可以为web页面添加许多有趣的东西。下面教大家如何用JavaScript写一个计时器。话不多说,直接上代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
<script type="text/javascript">
var attime;
var i;
function clock(){
var time=new Date();
attime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
document.getElementById("clock").value = attime;
}
function begin()
{
i=setInterval("clock()",1000);
}
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="100" />
<input type="button" value="Begin" onclick="begin()"/>
<input type="button" value="Stop" onclick="clearInterval(i)"/>
</form>
</body>
</html>
大家可以在自己电脑浏览器上运行试试,要想得到准确时间前提是你电脑系统时间正确。