Hello,
I am trying to program a cheating task: a participant has to sum an equation and provide the answer to the sum in a textbox that they can activate by spacebar. But the answer is "accidentally" shown after a few seconds, so they could cheat and view the answer before they press the spacebar.
We would like to measure the time until the spacebar is pressed. Here is the code I have so far, put together from various examples across the wiki - the error seems to be in the if condition, i've annotated it, but i don't understand why saving the time doesn't work in that spot :) I would appreciate any help, thanks!
<h1>15+16+-12-18+19</h2>
<h1 id="the_answer">42</h1>
(TX01)
(IN01)
<script type="text/javascript">
<!--
var frage = document.getElementById("TX01_qst"); // HTML-ID der Texteingabe
frage.style.display = "none";
var answer = document.getElementById("the_answer"); //this one is defined in the html above
answer.style.display = "none";
var timeStart = new Date(); //this correctly saves the current timestamp (checked)
document.addEventListener('keydown', function(event) {
if(event.keyCode == 32) { // space bar down
frage.style.display = ""; //makes the question appear
answer.style.display = "none"; //makes the answer disappear even if it had appeared for other reasons
var now = new Date(); //this should save the current timestamp when the key is pressed but does not work
}
});
// Function to show answer (ONLY if question is not displayed (i.e. has been activated by keypress)
function showanswer() {
if(frage.style.display == "none") {
answer.style.display = "";
document.getElementById("IN01_02").value = "1";
}
else {
document.getElementById("IN01_02").value = "0";
}
}
// Initialization of forwarding
SoSciTools.attachEvent(window, "load", function(evt) {
window.setTimeout(showanswer, 3000);
});
var time_untilspace = Math.floor((now.getTime() - timeStart.getTime()) / 1000); // this one should calculate the time passed between start of the task and the keypress (does not work because the time is not saved in the keypress if condition probably
// saving the variable
var input = document.getElementById("IN01_01");
input.value = time_untilspace;
// -->
</script>