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

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

背景:

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

问题

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

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <!-- Experiment Title -->
  <title>Experiment</title>

  <!-- Loading in libraries  -->
    <script src="https://unpkg.com/[email protected]"></script>
    <script src="https://unpkg.com/@jspsych/[email protected]"></script>
    <script src="https://unpkg.com/@jspsych/[email protected]"></script>
    <link href="https://unpkg.com/[email protected]/css/jspsych.css" rel="stylesheet" type="text/css" />   
</head>

<!-- Determing general page style parameters -->
<body style="background-color: rgb(200, 200, 200);">
<script>

    // Initiating jsPsych
    var jsPsych = initJsPsych();
  
    // Creating an empty array which will act as our timeline
    var timeline = [];

    // | - - - - - Consent - - - - - -|
    // Giving a simple consent event, in which participants select one of two buttons (values corresponding to '0' and '1'. from left to right)
    var consent = {
      type: jsPsychHtmlButtonResponse,
      stimulus: 'Consent Information',
      choices: ['I consent to participanting<br>in this study', 
                'I do not consent to participating<br>in this study'],
      render_on_canvas:false,
      data: {task: 'Consent'},
    }

    // Adding the consent event to the timeline 
    timeline.push(consent);
  
    // (Theoretically) saving the output value of the consent event to a variable named 'consented' 
    var consented = jsPsych.data.get().last(1).select('response')

    // 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. 
    // var test = {
    //     type: jsPsychHtmlKeyboardResponse,
    //     stimulus: consented,
    //     choices: [' '],
    //     data: {task: 'Instruction'},
    //   };
    //   timeline.push(test);

  // If the participant consents ...
  if (consented == '0'){

    // ... then show this message below.
    var instr_Consented = {
      type: jsPsychHtmlKeyboardResponse,
      stimulus: 'You consented!',
      choices: [' '],
      data: {task: 'Instruction'},
    };
    timeline.push(instr_Consented);
    }
  
  // ... but if the person doesn't consent...
  else {

    // ... show this message instead. 
    var instr_Consented = {
      type: jsPsychHtmlKeyboardResponse,
      stimulus: 'You did not consent!',
      choices: [' '],
      data: {task: 'Instruction'},
    };
    timeline.push(instr_Consented);
  }   

    // Executing the timeline
    jsPsych.run(timeline);

</script>
</body>
</html>
xuo3flqw

xuo3flqw1#

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

const consented_timeline = {
    timeline: [instr_Consented],
    conditional_function: () => {
        var consented = jsPsych.data.get().last(1).select('response')
        return consented == '0'
    }
}

const not_consented_timeline = {
    timeline: [instr_NotConsented],
    conditional_function: () => {
        var consented = jsPsych.data.get().last(1).select('response')
        return consented != '0'
    }
}

timeline.push(consented_timeline, not_consented_timeline);

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

7bsow1i6

7bsow1i62#

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

// Asking people whether they consent to study participation
    var consent = {
      type: jsPsychImageButtonResponse,
            stimulus: 'Consent Information',
      choices: ['I consent to participanting<br>in this study', 
                'I do not consent to participating<br>in this study'],
      data: {task: 'Consent'},
      on_finish: function (data) {

        // Check the participant's response, if it's the second option
        if (data.response == 1) {
    
          // ... end the experiment
          jsPsych.endExperiment('You did not consent!');
        }
      }
    }

    // Adding the consent event to the timeline 
    timeline.push(consent);
  
    // Otherwise, continue as normal
    var instr_Consented = {
      type: jsPsychHtmlKeyboardResponse,
      stimulus: 'You consented!',
      choices: [' '],
      data: {task: 'Instruction'},
    };
    timeline.push(instr_Consented);
    }

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

相关问题