qq_GO阿斯顿GO_0
2019-04-24 19:59
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num=num+1;
setTimeout("startCount()",1000);
}
window.onload=startCount;
</script>
</head>
<body>
<form>
<input type="text" id="count">
</form>
</body>
</html>
这个编辑器问题,你下载个其他编辑器试试,我用DW和Brackets都可以运行
这个编译器的问题,VS code 实测并不会出问题 代码如下:
<!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>
var num = 0;
function startCount() {
document.getElementById("count").value = num;
num = num + 1;
setTimeout(startCount,1000)
}
</script>
</head>
<body>
<input type="text" id="count" />
<input type="button" value="开始计数" onclick="startCount()" />
</body>
</html>
你要把 setTimeout("startCount()",1000); 放在startCount()外面调用,不然同时调用2次
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题