如果事件没有发生,jquery/javascript如何记录反应时间?
我的功能是记录从刺激(文字或图片)显示到按键的反应时间。 但是如果没有按下任何键,反应时间将为NaN,但我希望它一直运行到下一个刺激出现(这里是1000毫秒之后),因此即使没有按下任何键,反应也等于1000毫秒: 如果没有按键事件,如何记录毫秒到1000毫秒如果事件没有发生,jquery/javascript如何记录反应时间?,javascript,jquery,time,Javascript,Jquery,Time,我的功能是记录从刺激(文字或图片)显示到按键的反应时间。 但是如果没有按下任何键,反应时间将为NaN,但我希望它一直运行到下一个刺激出现(这里是1000毫秒之后),因此即使没有按下任何键,反应也等于1000毫秒: 如果没有按键事件,如何记录毫秒到1000毫秒 var t1; var i = 0; $(function(){ var timeout = 0; function showNext() { t1 = (new Date()).getTime();
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);
}
});
});
var-t1;
var i=0;
$(函数(){
var超时=0;
函数showNext(){
t1=(新日期()).getTime();
if(Math.random()<0.5){
var new_word=stim[Math.floor((Math.random()*stim.length)+1)].name;
$(“#abc”)。文本(新单词);
}否则{
var new_img=stim[Math.floor((Math.random()*stim.length)+1)]路径;
$(“#abc”).empty();
var prox_img=$('10){
返回;
};
i++;
clearTimeout(超时);
如果(e.which==97 | | e.which==108 | | e.which==32){
setTimeout(函数(){showNext();},100);
var t2=(新日期()).getTime();
var反应时间=t2-t1;
$(“#时间”).text(到达时间);
}
});
});
在showNext()
的开头,只需检查显示的时间是否为NaN
,并将其设置为1000
function showNext() {
if( isNaN($("#time").text()) ){
$("#time").text("1000");
}
//... your other code
}
See非常有效,谢谢!你知道我如何将功能流程(这里是10次)从按键中分离出来,这样就可以有10次刺激演示,10次反应时间,完全不管按键是什么吗?或者我应该在一个新问题中问这个问题吗?谢谢。