Conditional end of experiment #3244
-
Hi, I have an experiment to run on Prolific with a first section of questionnaires and later a behavioural task. My code structure is as follows. // Set up
var jsPsych = initJsPsych({
show_progress_bar: true,
auto_update_progress_bar: false,
message_progress_bar: 'Section Progress',
override_safe_mode: true,
on_finish: function () {
// window.location = "https://app.prolific.co/submissions/XXXXXXX" // commented out for testing
window.location = "https://google.com"
}
});
const timeline = [];
// Intro trials - not shown
// Questionnaire trials - not shown
// Attention checks
var failed_attention_checks_end = {
type: jsPsychHtmlButtonResponse,
choices: ["Continue"],
stimulus: `<p>Sorry, you've failed the attention checks so the experiment will now end.</p>
<p>If something went wrong that explains why the attention checks were not completed, please contact the researchers through the Prolific messaging system.</p>
<p>Click 'Continue' to be redirected to Prolific.</p>`,
on_finish: function(data) {
jsPsych.endExperiment();
}
}
var if_attention_checks = {
timeline: [failed_attention_checks_end],
conditional_function: function(){
// if participant failed 2 or more attention checks end the experiment
var data = jsPsych.data.get().filterCustom(function(x){ return x.trial_type == 'survey-template' && x.failed_attention_checks > 0 });;
if(data.count() >= 2){
return true;
} else {
return false;
}
}
}
timeline.push(if_attention_checks)
// Rest of experiment - not shown
// Upload data to database
var upload_data_trial = {
type: jsPsychCallFunction,
async: true,
func: function (done) {
const experimentalData = jsPsych.data.get().json();
const xhr = new XMLHttpRequest();
xhr.open('POST', `write_data.php?pid=${subject_id}`);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = () => {
if (xhr.status === 200) {
const response = xhr.responseText;
console.log(response);
} else {
console.log("Couldn't connect to `write_data.php`");
}
done();
};
xhr.send(experimentalData);
}
}
var end = {
type: jsPsychHtmlButtonResponse,
on_start: function () {
jsPsych.setProgressBar(1);
},
stimulus: `<p>Thank you for taking part in the experiment!</p>
<p>You can now close your window!</p>`,
choices: ["Bye!"]
};
timeline.push(upload_data_trial);
timeline.push(end);
// Run experiment
jsPsych.run(timeline); My question is, how can I call my I have tried Any help would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This seems like the right approach to me. Was the |
Beta Was this translation helpful? Give feedback.
This seems like the right approach to me. Was the
upload_data_trial
variable defined before theif_attention_checks
one in the script?