一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

如何在 jsPsych 实验中实现分支逻辑:条件跳转

时间:2026-07-19 11:02:54 编辑:袖梨 来源:一聚教程网

本文详解如何在 jspsych 7.x 中正确实现基于被试响应的分支逻辑,重点解决“无法实时获取前序试次数据导致条件判断失效”的常见问题,并提供可复用的条件时间线(conditional timeline)方案。

本文详解如何在 jspsych 7.x 中正确实现基于被试响应的分支逻辑,重点解决“无法实时获取前序试次数据导致条件判断失效”的常见问题,并提供可复用的条件时间线(conditional timeline)方案。

在 jsPsych 实验开发中,分支逻辑(如知情同意后的路径分流)是高频需求。但初学者常陷入一个关键误区:在构建时间线(timeline)阶段就试图读取尚未发生的被试响应。正如问题代码所示,jsPsych.data.get().last(1).select('response') 被调用时,实验尚未运行,consent 试次根本未执行,因此返回空或无效对象(如 [object Object]),导致 if (consented == '0') 永远不成立,所有被试均落入 else 分支。

根本解法是放弃“静态预判”,改用 jsPsych 原生支持的 动态条件时间线(Conditional Timeline) —— 它在每次时间线节点执行前实时评估函数,确保能准确读取已记录的试次数据。

✅ 正确实现步骤

  1. 定义分支内容:先创建需条件触发的试次(如同意/不同意后的提示页);
  2. 封装为条件时间线对象:每个对象包含 timeline 数组和 conditional_function;
  3. 在 conditional_function 中安全获取响应数据:推荐使用 filter() 精准定位目标试次,而非依赖 .last(1)(易受时间线执行顺序干扰);
  4. 将条件时间线推入主时间线:jsPsych 会在运行时自动按序评估并执行匹配分支。

以下是优化后的完整示例代码:

// 初始化 jsPsychconst jsPsych = initJsPsych();// 主时间线数组const timeline = [];// 1. 知情同意试次const consentTrial = {  type: jsPsychHtmlButtonResponse,  stimulus: '<h3>知情同意书</h3><p>本研究已通过伦理审查。点击下方按钮表示您的选择:</p>',  choices: ['我同意参与本研究', '我不同意参与本研究'],  data: { task: 'consent' },  on_finish: function(data) {    // 可选:显式标记响应值(便于调试)    data.response_label = data.response === 0 ? 'consent' : 'no_consent';  }};timeline.push(consentTrial);// 2. 同意后显示的内容const consentedInstruction = {  type: jsPsychHtmlKeyboardResponse,  stimulus: '<h3>感谢您的同意!</h3><p>接下来将开始正式实验任务。</p>',  choices: [' '],  data: { task: 'post_consent' }};// 3. 未同意后显示的内容const notConsentedInstruction = {  type: jsPsychHtmlKeyboardResponse,  stimulus: '<h3>感谢您的关注</h3><p>您已退出本实验。如有疑问,请联系研究人员。</p>',  choices: [' '],  data: { task: 'exit' }};// 4. 构建条件时间线(关键!)// ✅ 使用 filter() 精准查找 consent 试次,避免 .last(1) 的不确定性const consentedTimeline = {  timeline: [consentedInstruction],  conditional_function: () => {    const consentData = jsPsych.data.get().filter({ task: 'consent' });    if (consentData.count() === 0) return false; // 尚未完成 consent,跳过    return consentData.last().response === 0; // 注意:button response 是数字索引(0=左按钮)  }};const notConsentedTimeline = {  timeline: [notConsentedInstruction],  conditional_function: () => {    const consentData = jsPsych.data.get().filter({ task: 'consent' });    if (consentData.count() === 0) return false;    return consentData.last().response !== 0;  }};// 5. 将条件时间线加入主时间线timeline.push(consentedTimeline, notConsentedTimeline);// 启动实验jsPsych.run(timeline);

⚠️ 关键注意事项

  • conditional_function 的执行时机:它在每个时间线节点即将运行前被调用,此时 consentTrial 已完成且数据已写入,因此可安全读取;
  • .filter({ task: 'consent' }) 优于 .last(1):因时间线中可能插入其他试次(如日志、调试页),last(1) 易错位;而 filter 通过元数据精准锁定目标试次;
  • 响应值类型:html-button-response 的 response 是数字索引(0 表示第一个按钮),非字符串 '0',比较时请用 === 0;
  • 容错处理:conditional_function 中应检查数据是否存在(如 consentData.count() > 0),避免因数据未就绪导致报错;
  • 扩展性:多分支场景(如 3 种选项)可叠加多个条件时间线,或使用单个 conditional_function 返回不同布尔值组合。

通过该方案,你不仅能可靠实现知情同意分流,还可将其模式迁移至任意条件跳转场景(如按反应时分组、按问卷得分进入不同任务模块等)。记住核心原则:分支逻辑必须延迟到数据真实产生之后执行,而非在时间线构建期硬编码判断。

热门栏目