慕尼黑的女孩
2019-01-17 19:04
<title>计时器</title>
<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num ++;
setTimeout(startCount,1000);
}
var count = setInterval(startCount,1000);
</script>
</head>
<body>
<form>
<input type="text" id="count" onclick = "startCount"/>
为甚么不是从0,1,2,3,这样计数,而是0,2,5,9这样计的,求指点
第14行写这句: window.onload=startCount;
<title>计时器</title> <script type="text/javascript"> var num=0; function startCount() { document.getElementById('count').value=num; num ++; setTimeout("startCount()",1000);//修改处 } var count = setTimeout("startCount()",1000);//修改处 </script> </head> <body> <form> <input type="text" id="count" onclick = "startCount"/>
楼上回答应该没实践过,看下我的代码,我认为应该是onclick = "startCount",的问题,你可以自己再尝试下。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num ++;
// setTimeout(startCount,1000);
setTimeout("startCount()",1000);
}
var count = setInterval(startCount,1000);
</script>
</head>
<body>
<input type="text" id="count" onclick="startCount()"/>
</body>
</html>
startCount()函数里面用了setTimeout,外面就不要再用setInterval,本来就无限循环计数了,你还要每隔一秒再运行一个无限循环计数,当然会跳,不但会跳,时间一长浏览器还会崩溃
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题