You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JsPsych中timelineVariable()显示undefined问题求助

问题分析与解决

你的问题出在两个核心点:

  • survey-text插件没有stimulus参数,你误用了其他插件的配置项,该插件需要通过questions数组内的prompt字段设置题目提示文本。
  • 使用jsPsych.timelineVariable()时,必须将trial包装在带有timeline_variables属性的timeline节点中,否则无法读取变量值,导致显示undefined。

修正后的代码示例1(对应你注释的思路)

<!DOCTYPE html>
<html>
  <head>
    <title>Math Quiz</title>
    <script src="jspsych/dist/jspsych.js"></script>
    <script src = "jspsych/dist/plugin-survey-text.js"></script>
    <link href="jspsych/dist/jspsych.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <script> 
    var jsPsych = initJsPsych();

    var mathQuestions = [
        {math: '2+2 = ?', correct: '4'},
        {math: '2x3 = ?', correct: '6'},
        {math: '5x7 = ?', correct: '35'}
    ];

    // 创建带变量的timeline节点
    var test = {
        timeline: [
            {
                type: jsPsychSurveyText,
                questions: [
                    {prompt: jsPsych.timelineVariable('math'), name: 'resp'}
                ]
            }
        ],
        timeline_variables: mathQuestions
    };

    jsPsych.run([test]);
    </script>
  </body>
</html>

修正后的代码示例2(对应你未注释的思路)

<!DOCTYPE html>
<html>
  <head>
    <title>Math Quiz</title>
    <script src="jspsych/dist/jspsych.js"></script>
    <script src = "jspsych/dist/plugin-survey-text.js"></script>
    <link href="jspsych/dist/jspsych.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <script> 
    var jsPsych = initJsPsych();

    var test = {
        timeline: [ 
            {
                type: jsPsychSurveyText,
                // 使用survey-text正确的配置参数
                questions: [
                    {prompt: jsPsych.timelineVariable('math') + ' = ?', name: 'resp'}
                ]
            }
        ],
        timeline_variables: [
            { math: '2+2'},
            { math: '3+5'},
            { math: '10+7'}
        ]
    }

    jsPsych.run([test]);
    </script>
  </body>
</html>

关键说明

  • survey-text插件的核心配置是questions数组,每个元素需包含prompt(题目文本)和name(响应数据的键名),这是插件识别的必填字段。
  • jsPsych.timelineVariable('变量名')必须嵌套在timeline数组的trial中,且外层节点必须声明timeline_variables属性,JsPsych会自动遍历变量数组,为每个trial注入对应的值。

内容的提问来源于stack exchange,提问作者mbeasle2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 07:32:46