Basically, I wanna make a game which counts number of space bar presses/button clicks in 10 seconds. I made a countdown timer, and counter both are working fine
I could not find a way to stop counting key-press (after timer stops) so i have used "disable' button but suggest me if any better options.
this is all i did but i want 10 trials of the same game and after each trial it should save the number of clicks in the variable trials/in the table.
var trials = {
left:{
trial_1:0,
trial_2:0,
trial_3:0,
trial_4:0,
trial_5:0
},
right:{
trial_1:0,
trial_2:0,
trial_3:0,
trial_4:0,
trial_5:0
}
}
var count_clicks= 0;
function myInterval(i) {
var interval = setInterval(function(){startInterval()}, 1000);
function startInterval(){
if(i <= 0){
clearInterval(interval);
$("#btn").prop('disabled', true);
} else{
i--;
$("#timer").text(i);
}
}
}
$("#btn").on("click",function(){
count_clicks++
$("#result").text(count_clicks);
if(count_clicks == 1){
myInterval(10);
}
});
document.body.onkeyup = function(e){
if (e.keyCode == 32){
$("#btn").click();
return false;
}
}
#btn{
height:30px;
width:100px;
}
tr, th, td{
border:1px solid black;
padding:5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<span id="timer"></span> seconds<br>
<input id="btn" type="submit" value="Click Me">
<span id="result"></span><br>
<br>
<table>
<tr>
<th>hand</th>
<th>trial 1</th>
<th>trial 2</th>
<th>trial 3</th>
<th>trial 4</th>
<th>trial 5</th>
</tr>
<tr>
<th>left</th>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<th>right</th>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>