mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-12 16:48:12 +00:00
add multi-stim-multi-response plugin
This commit is contained in:
parent
5a61462c60
commit
0af5bd3f2b
208
plugins/jspsych-multi-stim-multi-response.js
Normal file
208
plugins/jspsych-multi-stim-multi-response.js
Normal file
@ -0,0 +1,208 @@
|
||||
/**
|
||||
* jspsych-muli-stim-multi-response
|
||||
* Josh de Leeuw
|
||||
*
|
||||
* plugin for displaying a set of stimuli and collecting a set of responses
|
||||
* via the keyboard
|
||||
*
|
||||
* documentation: docs.jspsych.org
|
||||
*
|
||||
**/
|
||||
|
||||
(function($) {
|
||||
jsPsych["multi-stim-multi-response"] = (function() {
|
||||
|
||||
var plugin = {};
|
||||
|
||||
plugin.create = function(params) {
|
||||
|
||||
var trials = new Array(params.stimuli.length);
|
||||
|
||||
for (var i = 0; i < trials.length; i++) {
|
||||
trials[i] = {};
|
||||
trials[i].stimuli = params.stimuli[i];
|
||||
trials[i].choices = params.choices;
|
||||
// option to show image for fixed time interval, ignoring key responses
|
||||
// true = image will keep displaying after response
|
||||
// false = trial will immediately advance when response is recorded
|
||||
trials[i].continue_after_response = (typeof params.continue_after_response === 'undefined') ? true : params.continue_after_response;
|
||||
// timing parameters
|
||||
trials[i].timing_stim = params.timing_stim || -1; // if -1, then show indefinitely
|
||||
trials[i].timing_response = params.timing_response || -1; // if -1, then wait for response forever
|
||||
// optional parameters
|
||||
trials[i].is_html = (typeof params.is_html === 'undefined') ? false : params.is_html;
|
||||
trials[i].prompt = (typeof params.prompt === 'undefined') ? "" : params.prompt;
|
||||
}
|
||||
return trials;
|
||||
};
|
||||
|
||||
|
||||
|
||||
plugin.trial = function(display_element, trial) {
|
||||
|
||||
// if any trial variables are functions
|
||||
// this evaluates the function and replaces
|
||||
// it with the output of the function
|
||||
trial = jsPsych.pluginAPI.normalizeTrialVariables(trial);
|
||||
|
||||
// this array holds handlers from setTimeout calls
|
||||
// that need to be cleared if the trial ends early
|
||||
var setTimeoutHandlers = [];
|
||||
|
||||
// array to store if we have gotten a valid response for
|
||||
// all the different response types
|
||||
var validResponses = [];
|
||||
for(var i = 0; i < trial.choices.length; i++){
|
||||
validResponses[i] = false;
|
||||
}
|
||||
|
||||
// array for response times for each of the different response types
|
||||
var responseTimes = [];
|
||||
for(var i = 0; i < trial.choices.length; i++){
|
||||
responseTimes[i] = -1;
|
||||
}
|
||||
|
||||
// array for response keys for each of the different response types
|
||||
var responseKeys = [];
|
||||
for(var i = 0; i < trial.choices.length; i++){
|
||||
responseKeys[i] = -1;
|
||||
}
|
||||
|
||||
// function to check if all of the valid responses are received
|
||||
function checkAllResponsesAreValid(){
|
||||
for(var i = 0; i<validResponses.length; i++){
|
||||
if(validResponses[i] == false){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// function to end trial when it is time
|
||||
var end_trial = function() {
|
||||
|
||||
// kill any remaining setTimeout handlers
|
||||
for (var i = 0; i < setTimeoutHandlers.length; i++) {
|
||||
clearTimeout(setTimeoutHandlers[i]);
|
||||
}
|
||||
|
||||
// kill keyboard listeners
|
||||
jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
|
||||
|
||||
// gather the data to store for the trial
|
||||
var trial_data = {
|
||||
"rt": JSON.stringify(responseTimes);
|
||||
"stimulus": JSON.stringify(trial.stimuli);
|
||||
"key_press": JSON.stringify(responseKeys);
|
||||
};
|
||||
|
||||
jsPsych.data.write($.extend({}, trial_data, trial.data));
|
||||
|
||||
// clear the display
|
||||
display_element.html('');
|
||||
|
||||
// move on to the next trial
|
||||
if (trial.timing_post_trial > 0) {
|
||||
setTimeout(function() {
|
||||
jsPsych.finishTrial();
|
||||
}, trial.timing_post_trial);
|
||||
} else {
|
||||
jsPsych.finishTrial();
|
||||
}
|
||||
};
|
||||
|
||||
// function to handle responses by the subject
|
||||
var after_response = function(info) {
|
||||
|
||||
var whichResponse;
|
||||
for(var i = 0; i<trial.choices.length; i++){
|
||||
// TODO: this method will only work if keys are specified
|
||||
// as key codes. Generalize to characters.
|
||||
if(trial.choices[i].indexOf(info.key) > -1){
|
||||
whichResponse = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(validResponses[whichResponse] != true){
|
||||
validResponses[whichResponse] = true;
|
||||
responseTimes[whichResponse] = info.rt;
|
||||
responseKeys[whichResponse] = info.key;
|
||||
}
|
||||
|
||||
if (trial.continue_after_response) {
|
||||
|
||||
if(checkAllResponsesAreValid()){
|
||||
end_trial();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// flattened version of the choices array
|
||||
var allchoices = [];
|
||||
for(var i=0;i<trial.choices.length; i++){
|
||||
allchoices = allchoices.concat(trial.choices[i]);
|
||||
}
|
||||
|
||||
var whichStimulus = 0;
|
||||
|
||||
function showNextStimulus(){
|
||||
|
||||
// display stimulus
|
||||
if (!trial.is_html) {
|
||||
display_element.append($('<img>', {
|
||||
src: trial.stimuli[whichStimulus],
|
||||
id: 'jspsych-multi-stim-multi-response-stimulus'
|
||||
}));
|
||||
} else {
|
||||
display_element.append($('<div>', {
|
||||
html: trial.stimuli[whichStimulus],
|
||||
id: 'jspsych-multi-stim-multi-response-stimulus'
|
||||
}));
|
||||
}
|
||||
|
||||
//show prompt if there is one
|
||||
if (trial.prompt !== "") {
|
||||
display_element.append(trial.prompt);
|
||||
}
|
||||
|
||||
if (typeof trial.timing_stim[whichStimulus] !== 'undefined' && trial.timing_stim[whichStimulus] > 0) {
|
||||
var t1 = setTimeout(function() {
|
||||
// clear the display, or hide the display
|
||||
if(typeof trial.stimuli[whichStimulus + 1] !== 'undefined'){
|
||||
display_element.html('');
|
||||
// show the next stimulus
|
||||
whichStimulus++;
|
||||
showNextStimulus();
|
||||
} else {
|
||||
$('#jspsych-multi-stim-multi-response-stimulus').css('visibility','hidden');
|
||||
}
|
||||
|
||||
}, trial.timing_stim[i]);
|
||||
|
||||
setTimeoutHandlers.push(t1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// show first stimulus
|
||||
showNextStimulus();
|
||||
|
||||
// start the response listener
|
||||
var keyboardListener = jsPsych.pluginAPI.getKeyboardResponse(after_response, allchoices);
|
||||
|
||||
// end trial if time limit is set
|
||||
if (trial.timing_response > 0) {
|
||||
var t2 = setTimeout(function() {
|
||||
end_trial();
|
||||
}, trial.timing_response);
|
||||
setTimeoutHandlers.push(t2);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return plugin;
|
||||
})();
|
||||
})(jQuery);
|
Loading…
Reference in New Issue
Block a user