我目前正在尝试运行以下用 jsPsych 编写的代码:
var likingRatingTask_1 = {
timeline: [
{
type: 'image-button-response-with-name',
stimulus: jsPsych.timelineVariable('food'),
choices: likingScale,
prompt: "I would like to eat this food right now, in its presented quantity.",
render_on_canvas: true,
name: jsPsych.timelineVariable('name'),
save_trial_parameters: {
name: true
}
}
],
timeline_variables: [
{food: "Images/small_salad.jpeg", name: 'salad'},
{food: "Images/small_pear.jpeg", name:'pear'},
{food: "Images/small_lollipop.jpeg", name:'lollipop'},
{food: "Images/small_grapes.jpeg", name:'grapes'},
{food: "Images/small_cotton_candy.jpeg", name:'cotton candy'},
{food: "Images/small_cookie.jpeg", name:'cookie'},
{food: "Images/small_carrot.jpeg", name:'carrot'},
{food: "Images/small_cake.jpeg", name:'cake'},
{food: "Images/small_brownie.jpeg", name:'brownie'},
{food: "Images/small_apple.jpeg", name:'apple'},
{food: "Images/large_salad.jpeg", name:'salad'},
{food: "Images/large_pear.jpeg", name:'pear'},
{food: "Images/large_lollipop.jpeg", name:'lollipop'},
{food: "Images/large_grapes.jpeg", name:'grapes'},
{food: "Images/large_cotton_candy.jpeg", name:'cotton candy'},
{food: "Images/large_cookie.jpeg", name:'cookie'},
{food: "Images/large_carrot.jpeg", name:'carrot'},
{food: "Images/large_cake.jpeg", name:'cake'},
{food: "Images/large_brownie.jpeg", name:'brownie'},
{food: "Images/large_apple.jpeg", name:'apple'}
],
randomize_order: true,
on_timeline_finish: function() {
var imageNames = [{name: 'salad'}, {name: 'pear'}, {name: 'lollipop'}, {name: 'grapes'}, {name: 'cotton candy'}, {name: 'cookie'}, {name: 'carrot'}, {name: 'cake'}, {name: 'brownie'}, {name: 'apple'}]; //list the category names
var imageNames_shuffled = jsPsych.randomization.shuffle(imageNames); //shuffle the category names list
console.log(imageNames_shuffled);
var data = []; //create a data array
var leftovers = []; //create a leftovers array
var imagesNatural_temp = []; //create a temporary imagesNatural array
var imagesHealthy_temp = []; //create a temporary imagesHealthy array
for (let i = 0; i < imageNames_shuffled.length; i++) {
var responseData = jsPsych.data.getLastTimelineData().filter(imageNames_shuffled[i]).select('response').sum();
var pictureData = jsPsych.data.getLastTimelineData().filter(imageNames_shuffled[i]).select('stimulus').values;
var object = {
rating: responseData,
picture1: pictureData[0],
picture2: pictureData[1]
};
data.push(object);
};
console.log(data);
function matchedAssignment(item) {
var x = item;
data.shift();
function findMatch(element) {
return element.response==x.response;
};
data.findIndex(findMatch);
if (data.findIndex(findMatch)!=-1) {
var y = data.splice(data.findIndex(findMatch), 1);
imagesNatural_temp.push(x);
imagesHealthy_temp.push(y[0]);
} else {
var z = jsPsych.randomization.sampleWithoutReplacement([imagesNatural_temp, imagesHealthy_temp], 1);
z[0].push(x);
}
};
data.forEach(matchedAssignment);
}
};
finalTimeline.push(likingRatingTask_1);
当我执行 console.log(imageNames_shuffled) 时,我的控制台返回了一个包含 10 个元素的数组,这是应该的。但是,当我执行 console.log(data) 时,我的控制台返回一个只有 2 个元素的数组。就好像 on_timeline_finish 函数只执行两次,即使我希望它执行 10 次。有没有人知道可能会发生什么?如果是这样,您能在这里分享您的见解吗?