Hi Katie,
I’m not sure what went wrong with your experiment, it sounds like you were doing things correctly. If you are implementing this through Builder, you would put the JS code in the “End Routine” tab of a code component for the routine that you want to log the variables for.
If you’re putting the code directly into the JS (not generally recommended by the developers) then it should be placed in the function for the end of the routine. For example, I have a routine called “questions” which has this function which I added the recording of two variables, “stim_id” and “stim_type”:
function questionsRoutineEnd() {
//------Ending Routine 'questions'-------
for (const thisComponent of questionsComponents) {
if (typeof thisComponent.setAutoDraw === 'function') {
thisComponent.setAutoDraw(false);
}
}
psychoJS.experiment.addData('key_resp_2.keys', key_resp_2.keys);
if (typeof key_resp_2.keys !== undefined) { // we had a response
psychoJS.experiment.addData('key_resp_2.rt', key_resp_2.rt);
routineTimer.reset();
}
key_resp_2.stop();
//Variables from the spreadsheet to record manually
psychoJS.experiment.addData('stim_id', trials.trialList[trials.thisIndex]["stim_id"]);
psychoJS.experiment.addData('stim_type', trials.trialList[trials.thisIndex]["stim_type"]);
// the Routine "questions" was not non-slip safe, so reset the non-slip timer
routineTimer.reset();
return Scheduler.Event.NEXT;
}
Did you give the correct name for your TrialHandler? This is the name of your loop in the Builder interface. In the code above, the TrialHandler is named “trials”. You’ll need to change your code according to the name of your TrialHandler.
If this isn’t working for you (though it really should, if implemented correctly!) you can also try what is suggested by the developers, over here