Skip to content

Instantly share code, notes, and snippets.

@benreyn
Created November 20, 2015 16:39
Show Gist options
  • Select an option

  • Save benreyn/1b3c5577fdaa0ced9c2c to your computer and use it in GitHub Desktop.

Select an option

Save benreyn/1b3c5577fdaa0ced9c2c to your computer and use it in GitHub Desktop.
reaction game
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.round(Math.random() * 15)];
}
return color;
}
var clickedTime; var createdTime; var reactionTime;
document.getElementById("box").onclick = function() {
clickedTime = Date.now();
reactionTime = (clickedTime - createdTime) / 1000;
document.getElementById("yourTime").innerHTML = reactionTime
this.style.display = "none";
reset();
}
function reset() {
var time = Math.random() * 5000
setTimeout(function() {
if (Math.random() < 0.5) {
document.getElementById("box").style.borderRadius = "50%";
} else {
document.getElementById("box").style.borderRadius = "0";
}
var top = Math.random();
top = top * 300;
var left = Math.random();
left = left * 300;
document.getElementById("box").style.top = top + "px";
document.getElementById("box").style.left = left + "px";
document.getElementById("box").style.backgroundColor = getRandomColor();
document.getElementById("box").style.display = "block";
createdTime = Date.now();
}, time);
}
reset();
<!doctype html>
<html>
<head>
<title>Reaction Tester - The Game</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Test your Reflexes!</h1>
<p>Click on the boxes and circles as quickly as you can!</p>
<p class = "bold">Your time: <span id="yourTime">0</span>s</p>
<div id="box"></div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
body {
font-family:Verdana, Geneva, sans-serif;
}
#box {
width:200px;
height:200px;
background-color:red;
display:none;
position:relative;
}
.bold {
font-weight:bold;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment