<!DOCTYPE html ><html> <head> <title> new document </title> <meta charset="utf-8"/> <script type="text/javascript"> var timer=0; function stime() { document.getElementById("txt").value=timer; timer=timer+1; var i=setTimeout(stime,1000) } function otime() { clearTimeout(i); } </script> </head> <body> <form> <input type="text" id="txt" > <input type="button" value="开始计时" onclick="stime()"> <input type="button" value="计时结束" onclick="otime()"> </form> </body></html>
2 回答
已采纳
李晓健
TA贡献1036条经验 获得超461个赞
因为你的 i 是一个局部变量,只能在 stime里可以访问到,在otime里是访问不到的,把 i 定义到外面就好了
<script type="text/javascript">
var timer=0;
var i = null;
function stime(){
document.getElementById("txt").value=timer;
timer=timer+1;
i=setTimeout(stime,1000)
}
function otime(){
clearTimeout(i);
}
</script>添加回答
举报
0/150
提交
取消
