var startTime;
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color = color + letters[Math.floor(Math.random() * 16)];
}
return color;
}
function makeShapeAppear() {
var top = Math.random() * 400;
var left = Math.random() * 400;
var width = (Math.random() * 200) + 100;
document.getElementById("shape").style.width = width + "px";
document.getElementById("shape").style.height = width + "px";
document.getElementById("shape").style.top = top + "px";
document.getElementById("shape").style.left = left + "px";
document.getElementById("shape").style.display = "block";
if (Math.random() > 0.5) {
document.getElementById("shape").style.borderRadius = "50%";
} else {
document.getElementById("shape").style.borderRadius = "0%";
}
document.getElementById("shape").style.backgroundColor = getRandomColor();
startTime = new Date().getTime();
}
function shapeAfterDelay() {
setTimeout(makeShapeAppear, (Math.random() * 2000));
}
shapeAfterDelay();
document.getElementById("shape").addEventListener("click", function(js) {
document.getElementById("shape").style.display = "none";
var endTime = new Date().getTime();
var timeTaken = ((endTime - startTime) / 1000);
document.getElementById("timeTaken").innerHTML = timeTaken;
shapeAfterDelay();
});
我用 Javascript 制作了一个游戏,其中用户单击形状,浏览器显示他单击该形状所花费的时间。现在我还想确定用户在所有点击中花费的最少时间,但无法弄清楚如何做到这一点。
BIG阳
相关分类