【问题标题】:How to record reaction time if event is not happening jquery/javascript?如果事件没有发生 jquery/javascript,如何记录反应时间?
【发布时间】:2014-04-21 15:33:54
【问题描述】:

我的功能是记录从显示刺激(文字或图片)到按键的反应时间。 但是如果没有按键,反应时间将为 NaN,但我希望它一直运行到下一个刺激出现(这里是 1000 毫秒之后),这样即使没有按键的反应也等于 1000 毫秒:

如果没有按键事件,我如何记录毫秒直到 1000 毫秒?

var t1;
var i = 0;
$(function(){
    var timeout = 0;
    function showNext() {
        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()}, 1000);
    }
    $(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();}, 100);
            var t2 = (new Date()).getTime();
              var reac_time = t2-t1;
              $("#time").text(reac_time);
                }
    });
});

【问题讨论】:

    标签: javascript jquery time


    【解决方案1】:

    showNext()的开头,只检查显示的时间是否为NaN,设置为1000

    function showNext() {
         if( isNaN($("#time").text()) ){
             $("#time").text("1000");
         }
         //... your other code
    }   
    

    w3Schools isNaN()

    【讨论】:

    • 完美运行,谢谢!知道如何将功能流程(此处为 10 次)与按键分离,以便完全不管按键,都会有 10 个刺激呈现,10 个反应时间?还是我应该在一个新问题中问这个?还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多