Question

I'm stuck with this one and hope that anyone can help me.

The script picks a word and records the elapsed time (reaction time) between word appearance and keypress. My idea was to have one function that picks the word, set it as interval, and have this interval broken by keypress. The attributes "stim" and "type" are for further use and I got problems if I had them picked more than once.

Here it is on fiddle: Fiddle of my problem

It does not, however, work quite correct. The default reaction time (2000ms), should only be recorded when there is no keypress. But as it is now, its recorded each time.

Js code:

var teststim = [{
    stim: "A",
    type: "letter"
}, {
    stim: "B",
    type: "letter"
}, {
    stim: "1",
    type: "integer"
}, {
    stim: "2",
    type: "integer"
}];
var RT = [];
var Type = [];
var Stim = [];
var displayword = function () {
    stuff = teststim[Math.floor((Math.random() * teststim.length))];
    $("#present").fadeOut(1000, function () {
        $("#present").text(stuff.stim).fadeIn();
        t1 = (new Date()).getTime();
    });
    reac = 2000;
};
timing = setInterval(displayword, 2000);
$(document).keypress(function (e) {
    clearInterval(timing);
    var t2 = (new Date()).getTime();
    reac = t2 - t1;
    t1 = t2;
    timing = setInterval(displayword, 2000);
});
Was it helpful?

Solution

If I understood your question correctly, your mistake is here:

    reac = 2000; RT.push(reac); $("#RT").val(RT);

You push and show reaction time as soon as you show word to type.

Here is your fiddle fixed.

Key is to first show the word and then start counting and not do it concurrently.

By the way there is still a little problem with reseting t1 in wrong place, so if you hit the keypress before fadeOut, but after default reaction time has fired, you'll get result like "2200".

You can fix it by, for example, putting it out of fadeOut: fiddle

To make it also change every time you press a key you can modify it like that: fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top