<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script type="text/javascript">
var c=0;
var t;
function timedCount()
{
if(c>=20){stopCount();};
document.getElementById('txt').value=c;
c++;
t=setTimeout("timedCount()",100);
}
function stopCount()
{
clearTimeout(t);
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()"><input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>点击按钮的时候就可以停止 但是用if来判断c>20的时候就不能停止,哪里错了?
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script type="text/javascript">
var c=0;
var t;
function timedCount()
{
if(c>=20){stopCount();};
document.getElementById('txt').value=c;
c++;
t=setTimeout("timedCount()",100);
}
function stopCount()
{
clearTimeout(t);
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()"><input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>点击按钮的时候就可以停止 但是用if来判断c>20的时候就不能停止,哪里错了?
function timedCount()
{
if(c>=20){
stopCount();
}
else{
c++;
t=setTimeout("timedCount()",100);
}
document.getElementById('txt').value=c;
}这样就可以了 。
明白了,我没有终止timedCount()的执行。再追问一句,有没有什么方法可以终止指定的函数执行,比如stopfn(timedCount) ---我自己瞎编的,你懂的!
后面加一个
return false;