Function as stimuli parameter #193
Replies: 3 comments
-
The function gets evaluated at the start of each trial, which is why the behavior isn't what you were expecting. For the code below, supplying an array to the stimuli parameter generates 4 distinct trials with different numbers. var trials = {
type: 'single-stim',
stimuli: ['1','2','3','4'],
is_html: true
} But for this declaration: var trials = {
type: 'single-stim',
stimuli: function(){ return ['1','2','3','4'] },
is_html: true
} jsPsych interprets this as a single trial, because there is only one item passed to the stimuli parameter and it is not an array. So when the trial begins, the function is evaluated, and the stimulus is 4 different numbers. A workaround is a bit complicated, because arbitrary list lengths mean that the number of trials is not known in advance. So you'll have to use a loop. Here's a sketch of how that might work. (Disclaimer: I'm generating this without testing -- it may not be bug free) var digit_index = 0;
var digits = ['1','2','3','4'];
var trial = {
type: 'single-stim',
stimuli: function(){
var digit = digits[digit_index];
digit_index++;
return('<p>'+digit+'</p>');
},
is_html: true
}
var loop = {
chunk_type: 'while',
timeline: [trial],
continue_function: function(){
if(digit_index >= digits.length){
return false;
} else {
return true;
}
}
jsPsych.init({
experiment_structure: [loop]
}); |
Beta Was this translation helpful? Give feedback.
-
Thanks for the explanation! That makes sense. I'm not sure using while chunks would necessarily work for us since we also want to be able to terminate the experiment early based on the accuracy of the participant's answers. We figured it would be reasonable enough to pre-generate random arrays of digits up to a length of 15 and push conditional chunks to the experiment structure array. So far, this is doing what we want it to:
|
Beta Was this translation helpful? Give feedback.
-
Glad it is working! |
Beta Was this translation helpful? Give feedback.
-
We're trying to use jsPysch to code a backward digit span task, and it seems that the single-stim plugin can accept a function as the stimuli parameter, but it doesn't do what we expect it to. Normally, when it's given an array of HTML strings directly, it displays them in sequence, but when it's given a function that returns an array, it displays everything all at once. Is this intended, and are there any workarounds that let you make stimulus lists of arbitrary length in real time? For reference, this is the relevant part of our code (the referenced global variables and functions are initialized elsewhere):
Beta Was this translation helpful? Give feedback.
All reactions