html 将分支逻辑并入jsPsych(JavaScript)实验

tv6aics1  于 2023-09-28  发布在  Java
关注(0)|答案(2)|浏览(124)

背景:

大家好,我是JavaScript的新手,我想用jsPsych(v7.0)库中的工具来做一个心理实验。我构建了大部分我需要的东西,但我很难让分支逻辑或条件过滤工作。
具体而言,我需要受试者同意或不同意研究程序。如果他们不同意,他们需要被分支以查看一条消息,如果他们同意,他们需要查看另一条消息。
我已经为复制生成了一个简化的版本(见下文)

问题

我最初认为索引同意事件产生的价值将是最简单的解决方案,但我似乎无法索引或引用以前试验的数据。几乎每次,我索引的值都显示为[object Object],即使当我导出数据对象并查看我认为我正在索引的内容时,我希望从同意中看到的'0'值也在那里。下面包含的代码是我的最新尝试,我只是尝试使用var consented = jsPsych.data.get().last(1).select('response')命令将上一次试验的值存储在response列中。”为什么它不工作?**我下面的脚本从来不满足条件语句consented == '0',并且总是输出else下的分支。
我愿意接受其他分支解决方案,除了索引以前的试验值,如果有人有任何。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <!-- Experiment Title -->
  7. <title>Experiment</title>
  8. <!-- Loading in libraries -->
  9. <script src="https://unpkg.com/[email protected]"></script>
  10. <script src="https://unpkg.com/@jspsych/[email protected]"></script>
  11. <script src="https://unpkg.com/@jspsych/[email protected]"></script>
  12. <link href="https://unpkg.com/[email protected]/css/jspsych.css" rel="stylesheet" type="text/css" />
  13. </head>
  14. <!-- Determing general page style parameters -->
  15. <body style="background-color: rgb(200, 200, 200);">
  16. <script>
  17. // Initiating jsPsych
  18. var jsPsych = initJsPsych();
  19. // Creating an empty array which will act as our timeline
  20. var timeline = [];
  21. // | - - - - - Consent - - - - - -|
  22. // Giving a simple consent event, in which participants select one of two buttons (values corresponding to '0' and '1'. from left to right)
  23. var consent = {
  24. type: jsPsychHtmlButtonResponse,
  25. stimulus: 'Consent Information',
  26. choices: ['I consent to participanting<br>in this study',
  27. 'I do not consent to participating<br>in this study'],
  28. render_on_canvas:false,
  29. data: {task: 'Consent'},
  30. }
  31. // Adding the consent event to the timeline
  32. timeline.push(consent);
  33. // (Theoretically) saving the output value of the consent event to a variable named 'consented'
  34. var consented = jsPsych.data.get().last(1).select('response')
  35. // I used this as a means of viewing the consent output value to check if the previous argument worked. This has always output [object Oject] and I don't know if I'm indexing wrong or I can't view the value of an index in this way, or something about indexing in this way corrupts the value.
  36. // var test = {
  37. // type: jsPsychHtmlKeyboardResponse,
  38. // stimulus: consented,
  39. // choices: [' '],
  40. // data: {task: 'Instruction'},
  41. // };
  42. // timeline.push(test);
  43. // If the participant consents ...
  44. if (consented == '0'){
  45. // ... then show this message below.
  46. var instr_Consented = {
  47. type: jsPsychHtmlKeyboardResponse,
  48. stimulus: 'You consented!',
  49. choices: [' '],
  50. data: {task: 'Instruction'},
  51. };
  52. timeline.push(instr_Consented);
  53. }
  54. // ... but if the person doesn't consent...
  55. else {
  56. // ... show this message instead.
  57. var instr_Consented = {
  58. type: jsPsychHtmlKeyboardResponse,
  59. stimulus: 'You did not consent!',
  60. choices: [' '],
  61. data: {task: 'Instruction'},
  62. };
  63. timeline.push(instr_Consented);
  64. }
  65. // Executing the timeline
  66. jsPsych.run(timeline);
  67. </script>
  68. </body>
  69. </html>
xuo3flqw

xuo3flqw1#

示例代码不起作用,因为当你到达if(consented == 0)条件时,实验还没有运行,所以consented不能反映受试者的实际React。jsPsych.run()在时间轴构建后被调用。
您可以使用条件时间轴来实现这一点。

  1. const consented_timeline = {
  2. timeline: [instr_Consented],
  3. conditional_function: () => {
  4. var consented = jsPsych.data.get().last(1).select('response')
  5. return consented == '0'
  6. }
  7. }
  8. const not_consented_timeline = {
  9. timeline: [instr_NotConsented],
  10. conditional_function: () => {
  11. var consented = jsPsych.data.get().last(1).select('response')
  12. return consented != '0'
  13. }
  14. }
  15. timeline.push(consented_timeline, not_consented_timeline);

您可能希望修改从数据中检索同意变量的方式,因为.last(1)在这种情况下不太可靠(例如,第二个条件时间轴可能在第一个时间线已经运行之后才被检查,因此最后一个试验将不再是同意试验)。我建议使用.filter()来选择特定的知情同意试验。

展开查看全部
7bsow1i6

7bsow1i62#

@乔希的回答似乎对我有效,但我只是想指出一个替代方案,我在看到他们的回应之前就想好了:

  1. // Asking people whether they consent to study participation
  2. var consent = {
  3. type: jsPsychImageButtonResponse,
  4. stimulus: 'Consent Information',
  5. choices: ['I consent to participanting<br>in this study',
  6. 'I do not consent to participating<br>in this study'],
  7. data: {task: 'Consent'},
  8. on_finish: function (data) {
  9. // Check the participant's response, if it's the second option
  10. if (data.response == 1) {
  11. // ... end the experiment
  12. jsPsych.endExperiment('You did not consent!');
  13. }
  14. }
  15. }
  16. // Adding the consent event to the timeline
  17. timeline.push(consent);
  18. // Otherwise, continue as normal
  19. var instr_Consented = {
  20. type: jsPsychHtmlKeyboardResponse,
  21. stimulus: 'You consented!',
  22. choices: [' '],
  23. data: {task: 'Instruction'},
  24. };
  25. timeline.push(instr_Consented);
  26. }

我发誓在发布问题之前我试过这个,它不起作用,但现在它似乎起作用了。我所做的就是在on_finish条件中添加一个函数,它可以结束实验,并在用户不同意时显示一条消息。同样,我真正纠结的是索引或调用值,看起来data.response在这里工作得很好。

展开查看全部

相关问题