added keys option to XAB

Added two optional parameters to the XAB plugin so that they key that
subjects press to answer left/right is a parameter. The parameters have
default values of 'q' and 'p' to maintain compatibility.
This commit is contained in:
Josh de Leeuw 2012-07-17 16:24:25 -04:00
parent 39846f1754
commit b8a72862cf

View File

@ -1,96 +1,96 @@
(function( $ ) { (function( $ ) {
jsPsych.xab = (function(){ jsPsych.xab = (function(){
var plugin = {} var plugin = {}
plugin.create = function(params) plugin.create = function(params)
{ {
//xab_stims = shuffle(xab_stims); //xab_stims = shuffle(xab_stims);
xab_stims = params["stimuli"]; xab_stims = params["stimuli"];
trials = new Array(xab_stims.length); trials = new Array(xab_stims.length);
for(var i = 0; i < trials.length; i++) for(var i = 0; i < trials.length; i++)
{ {
trials[i] = {}; trials[i] = {};
trials[i]["type"] = "xab"; trials[i]["type"] = "xab";
trials[i]["a_path"] = xab_stims[i][0]; trials[i]["a_path"] = xab_stims[i][0];
trials[i]["b_path"] = xab_stims[i][1]; trials[i]["b_path"] = xab_stims[i][1];
trials[i]["timing"] = params["timing"]; trials[i]["timing"] = params["timing"];
if(params["data"]!=undefined){ trials[i]["data"] = params["data"][i] || params["data"] || undefined;
trials[i]["data"] = params["data"][i]; trials[i]["left_key"] = params["left_key"] || 81; // defaults to 'q'
} trials[i]["right_key"] = params["right_key"] || 80; // defaults to 'p'
} }
return trials; return trials;
} }
plugin.trial = function($this, block, trial, part) plugin.trial = function($this, block, trial, part)
{ {
switch(part){ switch(part){
case 1: case 1:
p1_time = (new Date()).getTime(); p1_time = (new Date()).getTime();
$this.append($('<img>', { $this.append($('<img>', {
"src": trial.a_path, "src": trial.a_path,
"class": 'xab' "class": 'xab'
})); }));
setTimeout(function(){plugin.trial($this, block, trial, part + 1)}, trial.timing[0]); setTimeout(function(){plugin.trial($this, block, trial, part + 1)}, trial.timing[0]);
break; break;
case 2: case 2:
p2_time = (new Date()).getTime(); p2_time = (new Date()).getTime();
$('.xab').remove(); $('.xab').remove();
setTimeout(function(){plugin.trial($this, block, trial, part + 1)}, trial.timing[1]); setTimeout(function(){plugin.trial($this, block, trial, part + 1)}, trial.timing[1]);
break; break;
case 3: case 3:
p3_time = (new Date()).getTime(); p3_time = (new Date()).getTime();
startTime = (new Date()).getTime(); startTime = (new Date()).getTime();
var images = [trial.a_path, trial.b_path]; var images = [trial.a_path, trial.b_path];
var target_left = (Math.floor(Math.random()*2)==0); // binary true/false choice var target_left = (Math.floor(Math.random()*2)==0); // binary true/false choice
if(!target_left){ if(!target_left){
images = [trial.b_path, trial.a_path]; images = [trial.b_path, trial.a_path];
} }
// show the images // show the images
$this.append($('<img>', { $this.append($('<img>', {
"src": images[0], "src": images[0],
"class": 'xab' "class": 'xab'
})); }));
$this.append($('<img>', { $this.append($('<img>', {
"src": images[1], "src": images[1],
"class": 'xab' "class": 'xab'
})); }));
var resp_func = function(e) { var resp_func = function(e) {
var flag = false; var flag = false;
var correct = false; var correct = false;
if(e.which=='80') // 'p' key if(e.which== trial.left_key) // 'p' key
{ {
flag = true; flag = true;
if(!target_left) { correct = true; } if(!target_left) { correct = true; }
} else if(e.which=='81') // 'q' key } else if(e.which== trial.right_key) // 'q' key
{ {
flag = true; flag = true;
if(target_left){ correct = true; } if(target_left){ correct = true; }
} }
if(flag) if(flag)
{ {
endTime = (new Date()).getTime(); endTime = (new Date()).getTime();
rt = (endTime-startTime); rt = (endTime-startTime);
stim1_time = (p2_time-p1_time); stim1_time = (p2_time-p1_time);
isi_time = (p3_time-p2_time); isi_time = (p3_time-p2_time);
var trial_data = {"rt": rt, "correct": correct, "a_path": trial.a_path, "b_path": trial.b_path, "key_press": e.which, "key_press": e.which, "stim1_time": stim1_time, "isi_time":isi_time} var trial_data = {"rt": rt, "correct": correct, "a_path": trial.a_path, "b_path": trial.b_path, "key_press": e.which, "key_press": e.which, "stim1_time": stim1_time, "isi_time":isi_time}
block.data[block.trial_idx] = $.extend({},trial_data,trial.data); block.data[block.trial_idx] = $.extend({},trial_data,trial.data);
$(document).unbind('keyup',resp_func); $(document).unbind('keyup',resp_func);
$('.xab').remove(); $('.xab').remove();
setTimeout(function(){block.next();}, trial.timing[2]); setTimeout(function(){block.next();}, trial.timing[2]);
} }
} }
$(document).keyup(resp_func); $(document).keyup(resp_func);
//TODO: CHECK IF IMAGE SHOULD DISAPPEAR //TODO: CHECK IF IMAGE SHOULD DISAPPEAR
//based on timings //based on timings
break; break;
} }
} }
return plugin; return plugin;
})(); })();
})(jQuery); })(jQuery);