<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的时候就不能停止,哪里错了?
解决方案 »
- 请问,有两个radio,id和name都一样,value不一样
- 【雷人现象】虚拟机的IIS环境中JS文件已更新,结果却总是老版本
- JS验证日期,牛人指点下
- 请问一个抓取网页内容的问题
- 仿google搜索,成功了,但不支持键盘操作
- 怎样用JavaScript实现验证码的验证?
- 怎样才能在html中使用脚本中的变量,不是后台的,急~~~~~~~~~~~~~
- 大家帮我解释解释dw自动生成的js呀,解释通了高分相送。@_@
- 很简单的一个问题,关于页面table对象的。
- window.print()要显示对话框,能不能让它不显示呢?
- .net当中用Javascript给LI标签设置样式
- javascript 问题
function timedCount()
{
if(c>=20){
stopCount();
}
else{
c++;
t=setTimeout("timedCount()",100);
}
document.getElementById('txt').value=c;
}这样就可以了 。
明白了,我没有终止timedCount()的执行。再追问一句,有没有什么方法可以终止指定的函数执行,比如stopfn(timedCount) ---我自己瞎编的,你懂的!
后面加一个
return false;