慕斯卡6904759
2018-07-05 21:20
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
</head>
<script type="text/javascript">
var num=0;
function startCount() {
document.getElementById('count').value=num;
num=num+1;
setTimeout("startCount()",1000);
}
</script>
</head>
<body>
<form>
<input type="text" id="count" />
<input type="button" value="start" onclick="startCount()"/>
</form>
</body>
</html>
这个很有意思 当多点击几次按钮,计数会越来越快!!!
clearInterval(timer); 先清除一下定时器,document.getElementById('status').value = 2 设置值
clearInterval(timer);
document.getElementById('status').value = 2;
这个是什么意思呢?谢谢
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
</head>
<script type="text/javascript">
var num = 0;
var timer = null;
function startCount() {
var statusValue = document.getElementById('status').value;
if (statusValue == 1) {
clearInterval(timer);
document.getElementById('status').value = 2;
timer = setInterval(function () {
num = num + 1;
document.getElementById('count').value = num;
}, 1000);
document.getElementById('btn').value = "暂停";
} else {
clearInterval(timer);
document.getElementById('btn').value = "开始";
document.getElementById('status').value = 1;
}
}
</script>
</head>
<body>
<form>
<input type="hidden" id="status" value="1">
<input type="text" id="count">
<input id="btn" type="button" value="开始" onclick="startCount()">
</form>
</body>
</html>
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题