Why is stimulus showing as "undefined" #2747
-
For all of my scripts, the first stimuli in a sequence is showing as "undefined". See Minimal Worked Example (below). How can I prevent this happening? Thanks <!DOCTYPE html>
<html>
<head>
<title>My experiment</title>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/@jspsych/[email protected]"></script>
<script src="https://unpkg.com/@jspsych/[email protected]"></script>
<link href="https://unpkg.com/[email protected]/css/jspsych.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
<script>
/* initialize jsPsych */
var jsPsych = initJsPsych({
on_finish: function() {
jsPsych.data.displayData();
}
});
/* create timeline */
var timeline = [];
var stims = [
{"S1": "I like cheese"},
{"S1": "I like eggs"}
]
/* define welcome message trial */
var welcome = {
type: jsPsychHtmlKeyboardResponse,
stimulus: "Welcome to the experiment. Press any key to begin."
};
timeline.push(welcome);
/* define instructions trial */
var instructions = {
type: jsPsychHtmlKeyboardResponse,
stimulus: `
<p>Press any key to begin.</p>
`,
post_trial_gap: 2000
};
timeline.push(instructions);
var likert_scale = [
"Strongly Disagree",
"Disagree",
"Neutral",
"Agree",
"Strongly Agree"
];
var test = {
type: jsPsychSurveyLikert,
questions: [
{prompt: () => String(jsPsych.timelineVariable(`S1`)), labels: likert_scale}
],
randomize_question_order: true
};
timeline.push(test);
var test_procedure = {
timeline: [test],
timeline_variables: stims,
repetitions: 1,
randomize_order: true
};
timeline.push(test_procedure);
/* start the experiment */
jsPsych.run(timeline);
</script>
</html> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This one took me a while to figure out 😅 timeline.push(test); So when that first |
Beta Was this translation helpful? Give feedback.
This one took me a while to figure out 😅
The problem is that you've set up the
test
trial to run with timeline variables, but then you pushed it directly onto the main experiment timeline with this line:So when that first
test
trial runs, there are no timeline variables associated with the trial and the prompt function will returnundefined
. If you remove the line above, then the experiment will just run thetest
trials inside thetest_procedure
, so it will have access to your timeline variables.I hope that helps! Let us know if you have any other questions.