有點(diǎn)Bug。。點(diǎn)了stop后停止,再點(diǎn)2次start后再點(diǎn)stop就停止不了了....
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
i=setInterval("clock()",1000);
? ?function clock(){
? ? ? var time=new Date(); ? ? ? ? ? ? ? ?
? ? ? document.getElementById("clock").value = time;
? ?}
? ? ?
function mystart(){
i=setInterval("clock()",1000);
}
? ? function myclear(){
? ? ? ? clearInterval(i);
? ? }
</script>
</head>
<body>
? <form>
? ? <input type="text" id="clock" size="50" ?/>
? ? <input type="button" value="Stop" onclick="myclear()" />
<input type="button" value="Start" onclick="mystart()" />
? </form>
</body>
</html>
2015-09-27
是的,謝謝樓上的回答~^O^ ? 給我了一個(gè)思路。你這個(gè)代碼運(yùn)行后如果先點(diǎn)一次start后還是停止不了...
下邊是在你基礎(chǔ)上改進(jìn)的:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
var i=setInterval("clock()",1000);
var count = 0;
? ?function clock(){
? ? ? var time=new Date(); ? ? ? ? ? ? ? ?
? ? ? document.getElementById("clock").value = time;
? ? ? console.log(time);
? ?}
? ?function mystart(){
? ? if(count > 0 ){
? ? ? ?i=setInterval("clock()",1000);?
? count--;
? ? }
}
? ? function myclear(){
? ? ? ? clearInterval(i);
? ? ? ? count = 1;
? ? }
</script>
</head>
<body>
? <form>
? ? <input type="text" id="clock" size="50" ?/>
? ? <input type="button" value="Stop" onclick="myclear()" />
<input type="button" value="Start" onclick="mystart()" />
?
? </form>
</body>
</html>
2015-09-27
這個(gè)問題是,你點(diǎn)擊了多次開始。想到開啟了多個(gè)定時(shí)器。而你在關(guān)的時(shí)候只關(guān)了一個(gè)。所以會(huì)導(dǎo)致關(guān)不了。
這樣是一個(gè)不優(yōu)雅的解決辦法