為何停止后重新計(jì)時(shí)就不生效呢
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
? ?function clock(){
? ? ? var time=new Date();? ? ? ? ? ? ? ? ??
? ? ? document.getElementById("clock").value = time;
? ?}
? ? var i=setInterval(clock,100);
</script>
</head>
<body>
? <form>
? ? <input type="text" id="clock" size="50"? />
? ? <input type="button" value="Stop" onclick="clearInterval(i)" />
? ? <input type="button" value="Start" onclick="clock()">
? </form>? ?
</body>
</html>
2022-12-26
你start按鈕給的函數(shù)是clock(),而clock()只記錄你點(diǎn)擊按鈕那一瞬間的時(shí)間;要想繼續(xù)計(jì)時(shí)就得加上setInterval()函數(shù)每隔1秒刷新一次顯示時(shí)間。
2022-04-07
2021-11-26
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
? ?function clock(){
? ? ? var time=new Date(); ? ? ? ? ? ? ? ? ? ?
? ? ? document.getElementById("clock").value = time;
? ?}
? ?var i = setInterval(clock, 1000);
? ?function start(){
? ? ? ?i = setInterval(clock, 1000);
? ?}
</script>
</head>
<body>
? <form>
? ? <input type="text" id="clock" size="50" ?/>
? ? <input type="button" value="Stop" ?onclick="clearInterval(i)"/>
? ? <input type="button" value="Start" onclick="start()"/>
? </form>
</body>
</html>
2021-07-05
wujie