ヘッダーの行の何が問題になっていますか?
以下の例は、クリックされるたびにカウンターをインクリメントするボタンを作成することになっています。ただし、ボタンのクリック間に 2000 ミリ秒の遅延を強制します。以下のバージョンは機能しますが、代わりにコメントアウトされた行を使用すると
document.getElementById("rollButton").onclick=function(){calculation()};
(どちらも afterWaiting() 関数内)
さまざまな奇妙な結果が得られます。たとえば、カウンターが 1 よりも多く増加し始め、待機時間が消えるなどです。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function afterWaiting()
{
$("#rollButton").css("color","black");
//$("#rollButton").click(function(){calculation()});
document.getElementById("rollButton").onclick=function(){calculation()};
}
var counter=0;
function calculation()
{
////Enforcing wait:
document.getElementById("rollButton").style.color="red";
document.getElementById("rollButton").onclick="";
window.setTimeout("afterWaiting()",2000);
counter=counter+1;
document.getElementById("test").innerHTML=counter;
}
</script>
</head>
<body>
<button type="button" onclick="calculation()" id="rollButton"> Roll! </button>
<p id="test"> </p>
</body>
</html>
私は何を誤解しましたか?
前もって感謝します :)
JSFiddle: http://jsfiddle.net/Bwxb9/