Zamomin
Zamomin

Reputation: 57

Use user input from previous trial as stimulus in a new trial

I'm trying to program an experiment with the JavaScript package JSPsych. The MWE of my experiment is supposed to do the following: The participant enters a letter in a survey-text trial and in the following html-response trial, this letter is supposed to be presented to the participant as a stimulus.

My problem is that I can't find a way to access the input of the participant in the survey-text trial. My program structure follows the reaction time tutorial on the JSPsych website: The trials are being created one after another and pushed into a timeline array which is being initiated at the very end of the code. What that means is that at the time the trial is being defined the user has not input anything yet.

My MLE code can be found below. While running the experiment in a browser I am able to access the data from the survey-text trial with JSON.parse(jsPsych.data.getLastTrialData().select('responses').values).favorite. However, in the code below this produces the following error: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data, because (I strongly presume) there is no last trial at the time the code is being compiled.

let letter_display, letter_survey, test_procedure, timeline;
timeline = [];

letter_survey = {
    type: "survey-text",
    questions: [
        {prompt: "What is your favorite letter?", name:"favorite"}
                    ],
        }
timeline.push(letter_survey);

letter_display = {
    type: 'html-keyboard-response',
    stimulus: jsPsych.timelineVariable('stimulus'),
};

test_procedure = {
    timeline: [letter_display],
    timeline_variables: [{type:'html-keyboard-response', stimulus: JSON.parse(jsPsych.data.getLastTrialData().select('responses').values).favorite}]
};
timeline.push(test_procedure);

jsPsych.init({
    timeline: timeline
})

Upvotes: 0

Views: 1042

Answers (2)

Rankie Jiang
Rankie Jiang

Reputation: 1

Let me add another way

The on_start of the trial also works! It can change the stimulus or anything parameters of the trial based on the last trial's repsonse.

letter_display = {
    type: 'html-keyboard-response',
    stimulus:''
    on_start:function(trial){
       trial.stimulus =jsPsych.data.getLastTrialData().select('responses').values.favorite
    }
};
timeline.push(letter_display);

(if you do not assign the stimulus before the on_start, it can work, but with error message in console)

Upvotes: 0

Zamomin
Zamomin

Reputation: 57

I have found a solution. The part of the code where a value is assigned to the letter_display variable has to be changed as follows:

letter_display = {
    type: 'html-keyboard-response',
    stimulus: JSON.parse(jsPsych.data.getLastTrialData().select('responses').values).favorite
};
timeline.push(letter_display);

All code relating to the test_procedure variable can be scraped.

I.e. I got this to work for a "flat" timeline, but not a nested one. If anyone has a nested timeline solution, I would appreciate if they posted it as an answer here.

As a side note: Using JSON.parse(jsPsych.data.get().filter( {trial_type: 'survey-text'} ).select('responses').values).favorite generalizes to cases where the user input is not from the last trial.

Upvotes: 1

Related Questions