增加了一個(gè)“start”按鈕,如何實(shí)現(xiàn)點(diǎn)擊“start”按鈕后開始計(jì)時(shí),而點(diǎn)擊“stop”按鈕后結(jié)束計(jì)時(shí)呢?下面的代碼實(shí)現(xiàn)不了,只能實(shí)現(xiàn)點(diǎn)擊“stop”結(jié)束計(jì)時(shí),但點(diǎn)擊“start”只跳出一次時(shí)間,而無法繼續(xù)計(jì)時(shí)。。。請(qǐng)高手指點(diǎ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;
? ?function start(){
? ? ? var time=new Date(); ? ? ? ? ? ? ? ?
? ? ? document.getElementById("myclock").value = time;
// ? ? ?i=setInterval("start()",1000);?
? ?}
? ? i=setInterval("start()",1000);?
? ?function stop(){
// ? ?i=setInterval("start()",1000);
? ? ?clearInterval(i);
? ?}
</script>
</head>
<body>
? <form>
? ? <input type="text" id="myclock" size="50" ?/>
? ? <input type="button" value="Start" onclick="start()" />
? ? <input type="button" value="Stop" onclick="stop()" />
? </form>
</body>
</html>
2018-05-31
2018-05-29
2018-05-29
2018-05-03
你這個(gè)代碼嵌套有問題吧
2018-04-27
貌似要用到下面講的setTimeout和clearTimeout才可以。
<!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;
? ?function start(){
? ? ? var time=new Date(); ? ? ? ? ? ? ? ?
? ? ? document.getElementById("myclock").value = time;
? ? ? i=setTimeout("start()",1000);?
? ?}
// ? i=setTimeout("start()",1000);
? ?function stop(){
? ? ?clearTimeout(i);
? ?}
</script>
</head>
<body>
? <form>
? ? <input type="text" id="myclock" size="50" ?/>
? ? <input type="button" value="Start" onclick="start()" />
? ? <input type="button" value="Stop" onclick="stop()" />
? </form>
</body>
</html>