【发布时间】: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