本文详解如何在 jsPsych 7.x 中正确实现条件分支(如知情同意后的流程分流),指出常见错误(如提前读取未生成的数据),并提供基于 conditional_function 的健壮解决方案,含可复用代码示例与关键注意事项。
本文详解如何在 jspsych 7.x 中正确实现条件分支(如知情同意后的流程分流),指出常见错误(如提前读取未生成的数据),并提供基于 `conditional_function` 的健壮解决方案,含可复用代码示例与关键注意事项。
在 jsPsych 实验开发中,分支逻辑(branching logic)是构建个性化、响应式实验流程的核心能力——例如根据被试在知情同意环节的选择(“同意”或“拒绝”),动态跳转至不同后续任务。但初学者常陷入一个典型误区:在调用 jsPsych.run(timeline) 之前,就试图从 jsPsych.data.get() 中读取尚未发生的试验数据。正如问题代码所示,var consented = jsPsych.data.get().last(1).select('response') 这行语句执行时,整个实验尚未启动,last(1) 返回的是空数据集或默认占位对象(表现为 [object Object]),导致 if (consented == '0') 恒为 false,所有被试均落入 else 分支。
正确的做法是将分支决策延迟至运行时(runtime),利用 jsPsych 内置的 Conditional Timeline 机制。它允许你为任意时间线节点定义 conditional_function:该函数在该节点即将执行前自动调用,此时 consent 试验的数据已真实写入数据存储,可安全访问。
以下是优化后的完整实现方案(兼容 jsPsych v7+):
// 1. 定义知情同意试验
const consent = {
type: jsPsychHtmlButtonResponse,
stimulus: '<h3>知情同意书</h3><p>本研究已通过伦理审查。您有权随时退出...</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill9881" title="Json Repair Kit"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20dbae7c507.jpg" alt="Json Repair Kit" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill9881" title="Json Repair Kit" class="overflowclass">Json Repair Kit</a>
<p class="overflowclass">使用 Node.js 评估修复格式错误的 JSON,自动纠正尾随逗号、单引号、未加引号的键等常见问题,实现文件规范化。</p>
</div>
<a rel="nofollow" href="/xiazai/skill9881" title="Json Repair Kit" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>',
choices: ['我自愿同意参与本研究', '我不同意参与本研究'],
data: { task: 'consent' },
on_finish: function(data) {
// (可选)记录明确的布尔标记,提升可读性
data.consented = data.response === 0; // 左按钮为0 → 同意
}
};
// 2. 定义分支后的内容(注意:此处仅声明,不立即推入 timeline)
const instr_Consented = {
type: jsPsychHtmlKeyboardResponse,
stimulus: '<h2>感谢您的同意!</h2><p>接下来将开始正式实验任务...</p>',
choices: [' '],
data: { task: 'post_consent' }
};
const instr_NotConsented = {
type: jsPsychHtmlKeyboardResponse,
stimulus: '<h2>我们尊重您的决定</h2><p>实验到此结束。感谢您的时间与关注。</p>',
choices: [' '],
data: { task: 'exit' }
};
// 3. 创建带条件判断的时间线节点(关键!)
const consented_branch = {
timeline: [instr_Consented],
conditional_function: function() {
// ✅ 安全获取:过滤出 task='consent' 的最新试验(非依赖顺序索引)
const consent_data = jsPsych.data.get().filter({ task: 'consent' }).last();
return consent_data && consent_data.response === 0;
}
};
const not_consented_branch = {
timeline: [instr_NotConsented],
conditional_function: function() {
const consent_data = jsPsych.data.get().filter({ task: 'consent' }).last();
return consent_data && consent_data.response === 1; // 明确检查右按钮
}
};
// 4. 构建最终时间线:先推入 consent,再推入两个条件分支节点
const timeline = [consent, consented_branch, not_consented_branch];
// 5. 启动实验
jsPsych.run(timeline);
关键要点与最佳实践:
- ✅ 避免 last(1) 硬索引:last(1) 依赖试验执行顺序,在复杂时间线(如嵌套、循环、并行)中极易失效。应始终使用 filter({task: 'xxx'}) 精确定位目标试验。
- ✅ conditional_function 必须返回布尔值:true 则执行其 timeline,false 则跳过。确保逻辑无歧义(如显式检查 response === 0 而非 == '0',避免类型转换风险)。
- ⚠️ 注意执行时机:conditional_function 在每个节点渲染前调用,因此 consent 试验必已执行完毕,数据可实时读取。
- ? 扩展建议:若需多级分支(如同意后按性别分组),可嵌套条件时间线,或使用 jsPsych.timelineVariable 结合 sample 插件实现更灵活的随机化分流。
通过以上方法,你不仅能可靠实现知情同意分支,更能掌握 jsPsych 动态流程控制的核心范式——让实验逻辑真正“响应”被试行为,而非静态预设。










