<!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);
}
setTimeout("startCount()",1000);
function stopCount(){
clearTimeout(i);
}
</script>
</head>
<body>
<form>
<input type="text" id="count" />
<input type="button" value="Start" onClick="startCount()" />
<input type="button" value="Stop" onClick="stopCount()" />
</form>
</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,i;
function startCount() {
document.getElementById('count').value=num;
num++;
i = setTimeout("startCount()",1000);
}
function stopCount(){
clearTimeout(i);
}
</script>
</head>
<body>
<form>
<input type="text" id="count" />
<input type="button" value="Start" onclick="startCount()" />
<input type="button" value="Stop" onclick="stopCount()" />
</form>
</body>
</html>
你这个代码不会点击触发函数。而是直接触发,因为你在startCount()下面又加了一个setTimeout("startCount()",1000);这样的定时器,所以它会隔1秒后直接执行你上面的这个startCount()函数,
所以你点击开始是没用的,但是你点击停止有用,然后再点击开始也是可以的
首先C应该是小写