我這段js計時器的開始和停止按鈕的代碼哪有錯誤?
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計時器</title>
<script type="text/javascript">
? ?function clock(){
? ? ? var time=new Date(); ? ? ? ? ? ? ? ? ? ??
? ? ? document.getElementById("clock").value = time;
? ?}
? ? ?var i=setInterval(function(){
? ? ? ? ?clock()
? ? ?},100)
? ? ?function clock2(){
? ? ? ? ?clearInterval(i)
? ? ?}
function clock3(){
var i=setInterval(function(){
clock()},100)
}
</script>
</head>
<body>
? <form>
? ? <input type="text" id="clock" size="50" ?/>
? ? <input type="button" value="Stop" onclick="clock2()" />
? ? <input type="button" value="Continue" onclick="clock3()">
? </form>
</body>
</html>
按了continue之后再按stop就無效了,該怎么改
2016-03-20
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計時器</title>
<script type="text/javascript">
? ? function clock(){
? ? ? var time=new Date(); ? ? ? ? ? ? ? ? ? ??
? ? ? document.getElementById("clock").value = time;
? ?}
? ? var i;
? ? function clock2(){
? ? ? ? ?clearInterval(i)
? ? ?}
? ? function clock3(){
? ? i=clearInterval(i)
i=setInterval(clock,100)
}
</script>
</head>
<body>
? <form>
? ? <input type="text" id="clock" size="50" onclick="clock3()"/>
? ? <input type="button" value="Stop" onclick="clock2()" />
? ? <input type="button" value="Continue" onclick="clock3()">
? </form>
</body>
</html>
多次摁的問題解決了 但是 上來還是不顯示時間。
2016-03-20
上面的代碼有bug;就是剛開始沒有顯示時間,你的先摁一下Continue才能觸發(fā)。還有一個bug就是你多次觸發(fā)Continue 再stop 不好使?你自己改一下。
2016-03-20
給一個全局變量。var = i;
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計時器</title>
<script type="text/javascript">
? ? function clock(){
? ? ? var time=new Date(); ? ? ? ? ? ? ? ? ? ??
? ? ? document.getElementById("clock").value = time;
? ?}
? ? var i;
? ? function clock2(){
? ? ? ? ?clearInterval(i)
? ? ?}
? ? function clock3(){
i=setInterval(clock,100)
}
</script>
</head>
<body>
? <form>
? ? <input type="text" id="clock" size="50" ?/>
? ? <input type="button" value="Stop" onclick="clock2()" />
? ? <input type="button" value="Continue" onclick="clock3()">
? </form>
</body>
</html>