jsPsych/jspsych-xab.js
Josh de Leeuw 4ea424b465 added variables option to jspsych-text
variables (in the form of functions) can be passed in to the text plug
in and their contents (return values) are substituted into the string
when the text plugin is displayed.

(also fixed bug in xab where correct/incorrect were backwards -- as of
jul 17 commit)
2012-07-26 16:02:11 -04:00

106 lines
2.9 KiB
JavaScript
Executable File

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