計時器多次點擊,每點擊一個開始就多一個計時器,而多個計時器同時工作的時候,每次單擊只能停止一個計時器。
<!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);
? }
? 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>
2018-06-22
解決多開定時器的方法只有一個,就是讓開關(guān)點擊完一次不能再次點擊。
憑老夫多年經(jīng)驗給你2段代碼,你把它們加上??document.getElementById("myButton").disabled = true;這段加在定時器打開的函數(shù)里面。。document.getElementById("myButton").disabled = false;這段加在定時器停止的函數(shù)里面。。? 順便你給打開定時的button起一個id=myButton
2018-06-22
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計時器</title>
<script type="text/javascript">
? var num=0;
? var i;
? var a = true;
function add(){
document.getElementById('count').value=num;
num=num+1;
i=setTimeout("add()",1000);
}
? function startCount(){
? if(a){
add()
? ? ? }
? a = false;
? }
??
? function stopCount(){
if(a==false){
clearTimeout(i);
}
a = true;
? }
</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>
這是我之前學(xué)習(xí)時寫的代碼,這個就是不變button的