From 0af5bd3f2bc2274a7a7297896fae93b246e00d1e Mon Sep 17 00:00:00 2001 From: Josh de Leeuw Date: Tue, 28 Oct 2014 11:49:09 -0400 Subject: [PATCH] add multi-stim-multi-response plugin --- plugins/jspsych-multi-stim-multi-response.js | 208 +++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 plugins/jspsych-multi-stim-multi-response.js diff --git a/plugins/jspsych-multi-stim-multi-response.js b/plugins/jspsych-multi-stim-multi-response.js new file mode 100644 index 00000000..94afe77d --- /dev/null +++ b/plugins/jspsych-multi-stim-multi-response.js @@ -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 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 -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', { + src: trial.stimuli[whichStimulus], + id: 'jspsych-multi-stim-multi-response-stimulus' + })); + } else { + display_element.append($('
', { + 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);