有點Bug。。點了stop后停止,再點2次start后再點stop就停止不了了....
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計時器</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^ ? 給我了一個思路。你這個代碼運行后如果先點一次start后還是停止不了...
下邊是在你基礎上改進的:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計時器</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
這個問題是,你點擊了多次開始。想到開啟了多個定時器。而你在關的時候只關了一個。所以會導致關不了。
這樣是一個不優雅的解決辦法