87,926
社区成员
发帖
与我相关
我的任务
分享
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount(p)
{
if(p && t>0)
{
return false;
}
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount(false)",1000);
}
function stopCount()
{
clearTimeout(t);
t=0;
c=0;
}
</script>
</head>
<body>
<form>
<input type="button" value="开始计时!" onClick="timedCount(true)">
<input type="text" id="txt">
<input type="button" value="停止计时!" onClick="stopCount()">
</form>
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
if(t!=null){clearTimeout(t);t=null;}
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
function stopCount()
{
if(t!=null){clearTimeout(t);t=null;}
c=0;
}
</script>
</head>
<body>
<form>
<input type="button" value="开始计时!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="停止计时!" onClick="stopCount()">
</form>
<p>请点击上面的“开始计时”按钮来启动计时器。输入框会一直进行计时,从 0 开始。点击“停止计时”按钮可以终止计时。 </p>
</body>
</html>
因为你每次点击按钮都会调用函数 而函数里面又会调用自身 所以就和到一起去了,这个你可以
当你点击开始计数 将计数按钮禁用来避免