私は現在Javascriptで初心者コースをやっており、本質的に反応タイマーである非常に基本的なプロジェクトに取り組んでいます。ランダムな形状(四角または円のみ)がランダムな間隔でランダムな位置に表示され、ユーザーがクリックする必要があるという考え方です。JavaScriptの2イベント間の時間を計算します
は、ここで私がこれまで持っているものです。
document.addEventListener("DOMContentLoaded", function() {
var shape = document.getElementById("shape");
function generateRandomShape() {
console.log("TEST");
var shapeType = ["50%", ""];
var shapeColor = ["red", "green", "yellow", "purple", "orange", "brown", "gray", "black", "blue"];
shape.classList.remove("disappear");
shape.style.backgroundColor = shapeColor[Math.floor(Math.random() * shapeColor.length)];
shape.style.height = Math.random() * 300 + "px";
shape.style.width = shape.style.height;
shape.style.borderRadius = shapeType[Math.floor(Math.random() * shapeType.length)];
shape.style.marginLeft = Math.random() * 700;
shape.style.marginTop = Math.random() * 300;
}
shape.onclick = function() {
var endTime = Date.now();
var randomDelay = Math.random() * 3000;
var startTime = endTime - randomDelay;
var reaction = (endTime - startTime)/1000;
setTimeout(generateRandomShape, randomDelay);
shape.className = "disappear";
document.getElementById("end").innerHTML = endTime;
document.getElementById("start").innerHTML = startTime;
document.getElementById("reactionTime").innerHTML = reaction;
}
generateRandomShape();
});
.disappear {
display: none;
}
<h1>Test Your Reactions!</h1>
<p>Click on the boxes and circles as quickly as you can!</p>
<p>Your time is: <span id="reactionTime"></span> seconds</p>
<div id="shape"></div>
<div id="start"></div>
<div id="end"></div>
<div id="reactionTime"></div>
それはすべてが反応タイマーを除くOK動作し、私はこの作業を取得する方法を考え出すことはできません。私は図形をクリックしてendTime変数を作成していますが、startTime変数の周りに頭を浮かべることはできません。どのようにシェイプが現れた時間を保存するのですか?明らかに私のここでの試みはうまくいかず、なぜ私は簡単に見ることができますが、私は何を入れなければならないのか分かりません。どのような助けが大いに感謝!