計(jì)時(shí)器程序無法停止計(jì)時(shí)
寫了好多遍,都是能開始就停不了,能停就不能開始,我希望能實(shí)現(xiàn)點(diǎn)擊start按鈕,text框(初始為null)開始計(jì)時(shí),點(diǎn)擊stop,計(jì)時(shí)結(jié)束。
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html?xmlns="http://www.w3.org/1999/xhtml"> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/> <title>無標(biāo)題文檔</title> <script?type="text/javascript"> function?gg_1(){ var?s=new?Date(); document.getElementById("time").value=s; } var?i=setInterval(gg_1,100); function?ss(){ clearInterval(i); } </script> </head> <body> <input?type="button"?id="start"?value="Start"?onclick="gg_1()"/> <input?type="text"?id="time"?/> <input?type="button"?id="stop"?value="Stop"?onclick="ss()"/> </body> </html>
2016-10-26
您想要的是計(jì)時(shí),而你寫的是時(shí)間的,有點(diǎn)矛盾,計(jì)時(shí)器用setTimeout()比較好,看下我的代碼:(注意看下注釋部分)
2017-09-04
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
? var num=0;
? var i;
? function startCount(){
? ? document.getElementById('count').value=num;
? ? num=num+1;
? ? i=setTimeout("startCount()",1000);
? }
? flag=0;
? function stopCount(){
? ? ? if(flag==0){
? ? ? ? ? flag=1;
? ? ? ? ? document.getElementById('but').value="Stop";
? ? ? startCount();
? ? ? }else{
? ? ? ? ? clearTimeout(i);
? ? ? ? ? flag=0;
? ? ? ? ? document.getElementById('but').value="Start";
? ? ? }
? }
? function ce(){
? ? ? if(flag==0){
? ? ? document.getElementById('count').value="清除完畢!";
? ? ? num=0;
? ? ? setTimeout("document.getElementById('count').value=num",1000);
? ? ? }
? }
</script>
</head>
<body>
? <form>
? ? <input type="text" id="count" />
? ? <input type="button" id="but" value="Start" onclick="stopCount()" />
? ? <input type="button" value="Clear" ?onclick="ce()" />
? </form>
</body>
</html>
2016-10-19
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script type="text/javascript">
? ? window.onload=function(){
? ? ? timer = setInterval("abc()",1000)
? ? }
? ??
? ? function abc(){
? ? ? ? var s=new Date();
? ? ? ? document.getElementById("time").value=s;
? ? }
? ? ?
? ? function bcd(){
? ? ? clearInterval(timer);
? ? }
? ? function efg(){
? ? ? timer = setInterval("abc()",1)
? ? }
</script>
</head>
<body>
<input type="button" id="start" value="Start" onclick="efg()"/>
<input type="text" id="time" />
<input type="button" id="stop" value="Stop" onclick="bcd()"/>
</body>
</html>
重寫了一下 這個(gè)好像可以了
2016-10-19
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script type="text/javascript">
function gg_1(){
? ? var s=new Date();
? ? document.getElementById("time").value=s;
}
function aaa(){
? ? setInterval(gg_1,100);
}
var i=setInterval(gg_1,100);
function ss(){ ? ?
? ? clearInterval(i);
}
</script>
</head>
?
<body>
<input type="button" id="start" value="Start" onclick="aaa()"/>
<input type="text" id="time" />
<input type="button" id="stop" value="Stop" onclick="ss()"/>
</body>
</html>
2016-10-17
woyebuhui