
js的计时器重复点击开始计时如何清除上一次的计时器
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>计时器</title><scripttype="text/ja...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>计时器</title>
<script type="text/javascript">
var c = 0;
var t;
var onOff=true;
function timedCount(){
document.getElementById('txt').value = c;
c = c+1;
t = setTimeout("timedCount()",1000);
}
function stopCount(){
clearTimeout(t);
}
function cs(){
c = 0;
document.getElementById('txt').value= 0;
}
</script>
</head>
<body>
<form>
<input id="btn1" type="button" value="开始计时" onclick="timedCount()">
<input type="text" id="txt">
<input type="button" value="停止计时" onclick="stopCount()">
<input type="button" value="重置" onclick="cs()">
</form>
<p>当点击“开始计时”的按钮时,从0开始一直进行计时,当点击“停止计时”按钮时停止计时。</p>
</body>
</html>
这个怎么改啊? 展开
<html>
<head>
<meta charset="utf-8">
<title>计时器</title>
<script type="text/javascript">
var c = 0;
var t;
var onOff=true;
function timedCount(){
document.getElementById('txt').value = c;
c = c+1;
t = setTimeout("timedCount()",1000);
}
function stopCount(){
clearTimeout(t);
}
function cs(){
c = 0;
document.getElementById('txt').value= 0;
}
</script>
</head>
<body>
<form>
<input id="btn1" type="button" value="开始计时" onclick="timedCount()">
<input type="text" id="txt">
<input type="button" value="停止计时" onclick="stopCount()">
<input type="button" value="重置" onclick="cs()">
</form>
<p>当点击“开始计时”的按钮时,从0开始一直进行计时,当点击“停止计时”按钮时停止计时。</p>
</body>
</html>
这个怎么改啊? 展开
1个回答
展开全部
点击开始计时时先调用停止计时函数和重置函数再调用开始计时函数就可以了。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>计时器</title>
<script type="text/javascript">
var c = 0;
var t;
var onOff = true;
function timedCount() {
document.getElementById('txt').value = c;
c = c + 1;
t = setTimeout("timedCount()", 1000);
}
function stopCount() {
clearTimeout(t);
}
function cs() {
c = 0;
document.getElementById('txt').value = 0;
}
</script>
</head>
<body>
<form>
<input id="btn1" type="button" value="开始计时" onclick="stopCount();cs();timedCount();">
<input type="text" id="txt">
<input type="button" value="停止计时" onclick="stopCount()">
<input type="button" value="重置" onclick="cs()">
</form>
<p>当点击“开始计时”的按钮时,从0开始一直进行计时,当点击“停止计时”按钮时停止计时。</p>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询