连枝
2016-10-17 13:28
写了好多遍,都是能开始就停不了,能停就不能开始,我希望能实现点击start按钮,text框(初始为null)开始计时,点击stop,计时结束。
<!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>无标题文档</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>
您想要的是计时,而你写的是时间的,有点矛盾,计时器用setTimeout()比较好,看下我的代码:(注意看下注释部分)
<!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>无标题文档</title> <script type="text/javascript"> var i; var num=0; function st(){ document.getElementById("time").value=num; num=num+1; i=setTimeout(st,1000); } function ss(){ clearTimeout(i); } //var i;//这里是第二种写法 //var num=0; //var n=true; //function gg_1(){ // document.getElementById("time").value=num; // num=num+1; // i=setTimeout(gg_1,1000); //} //function st(){ // if(n==true){ // clearTimeout(i);//这里如果把clearTimeout(i);num=0;注释掉,就和上面的类似;不注释掉,再次点击start时,会重新开始计时 // num=0; //但是,上面的方法会有个bug,就是,连续多次点击start按钮,计时会加速 // setTimeout(gg_1,1000); // n=false; // } // } // //function ss(){ // if(n==false){ // clearTimeout(i); // n=true; // } // } </script> </head> <body> <input type="button" id="start" value="Start" onclick="st()"/> <input type="text" id="time" /> <input type="button" id="stop" value="Stop" onclick="ss()"/> </body> </html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</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>
<!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>无标题文档</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>
重写了一下 这个好像可以了
<!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>无标题文档</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>
woyebuhui
JavaScript进阶篇
468191 学习 · 21891 问题
相似问题