0

My function below measures reaction time for a keypress event. It prints it on the screen in the 'time' div but only gives the last reaction time, and it should push each reaction time into an array (react_time_arr). What I would like to achieve is (lower part of my code):

a) that each reaction time (for 10 runs) is printed to the hidden field 'outp' which in the end stores 10 consecutive reaction times, and

b) that I can check the content of the hidden field in the 'time' div, and

c) that after 10 runs I see reaction time 8, 9, 10 in the 'proxy_print' div.

Can you help me?

HTML

<input type="hidden" id="outp" name="output" value="" />

    <div id="time">RT: </div>
    <div id="proxy_print"></div>

JS

var reac_time_arr = []; var t1; var i = 0;
var ISI = [100, 200, 300];
var SPT = 1000;
$(function(){
    var timeout = 0;
    function showNext() {
        if(isNaN($("#time").text()) ){
            $("#time").text("1000");
            }
        t1 = (new Date()).getTime();
            if(Math.random() < 0.5) {
            var new_word = stim[Math.floor((Math.random()*stim.length)+1)].name;
            $("#abc").text(new_word);
            } else {
            var new_img =  stim[Math.floor((Math.random()*stim.length)+1)].path;
            $("#abc").empty();
            var prox_img = $('<img id="abcimg" height="300px" width="300px">');
            prox_img.attr('src', new_img);
            prox_img.appendTo('#abc');
            }
        timeout = setTimeout(function(){showNext()}, SPT);
    }
    $(document).keypress(function(e){
            if ($(e.target).is('input, textarea') || i > 10) {
                return;
            };
        i++;
            clearTimeout(timeout);
            if (e.which === 97 || e.which === 108 || e.which === 32) {
              setTimeout(function(){showNext();}, ISI[Math.floor(Math.random() * ISI.length)]);
            var t2 = (new Date()).getTime();
              var reac_time = t2-t1;
            reac_time_arr.push(reac_time);
            $("#time").text(reac_time);
            $("#outp").val(reac_time_arr);
            $("#proxy_print").text((reac_time_arr[reac_time_arr.length-3]));
                }
    });
});
2
  • You should remove the +1 from the random selection
    – Bergi
    Mar 18, 2014 at 15:22
  • 1
    What is the ISI? Where is it defined? What does it contain?
    – Fabricio
    Mar 18, 2014 at 16:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.