如何讓button只能點擊一次
有bug啊,怎么才能讓“button"只能執(zhí)行一次,當(dāng)持續(xù)點擊button,則不是按照一秒執(zhí)行一次,而是,,,
<!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 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>
2015-09-16
<!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 i;
? function startCount(){
??? document.getElementById('count').value=num;
??? num=num+1;
??? i=setTimeout("startCount()",1000);
??? document.getElementById("Start").disabled=true;
? }
? function stopCount(){
? clearTimeout(i);
? document.getElementById("count").value="";
? document.getElementById("Start").disabled=false;
? }
</script>
</head>
<body>
? <form>
??? <input type="text" id="count" />
??? <input type="button" value="Start" id="Start" onclick="startCount()" />
??? <input type="button" value="Stop"? onclick="stopCount()" />
? </form>
</body>
</html>
????下劃線的部分是我和你不同的部分,我給開始按鈕加了一個ID,然后通過在函數(shù)中加了三個樣式,實現(xiàn)了開始按鈕點擊一次不可用,停止按鈕點擊后開始按鈕回復(fù),文本框清零。
2015-08-14
我也隱隱約約發(fā)現(xiàn)了這個問題,你說的是按多次start的話計時會變快是吧。判斷一下就行了,num=0的話就執(zhí)行函數(shù)里面的內(nèi)容。但問題根本原因是什么,這個就不太清楚。難道是多線程?OTZ
2015-07-30
function stopCount(){
? num=0;
? clearTimeout(i);
? }
加一句num=0;
2015-07-06
var gg=true;
function startCount(){
? ? document.getElementById('count').value=num;
? ? num=num+1;
if(gg){
gg=false;?
?? i=setTimeout(function(){
startCount();
gg=true;
},1000);
}