Experiment sometimes not sending data

77 views
Skip to first unread message

Julie Gerard

unread,
Oct 1, 2020, 7:40:11 AM10/1/20
to jsPsych
Hello,

I've run 2 iterations of an experiment on Prolific, and the first one sent the data to my server for 80% of the participants, while the second one sent the data for just 40% of the participants. For the participants where the data did send, I have all of their data for all of the trials; it's the ones that didn't send any data that I can't figure out - there is no data file at all for these participants.

There is no javascript error when the 'submit' button is clicked, althought I do sometimes get the error when I start the experiment "The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page." I didn't think this should be an issue, though, since there is not audio input from the participant in the experiment.

All of the participants must have clicked 'submit', since this was the only way to return to Prolific via the url with the completion code.
Could this be a browser issue?

Here is my code for sending the data to the server, and the jspsych.init portion:

function saveData(name, data){
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'write_data.php'); // 'write_data.php' is the path to the php file described above.
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send(JSON.stringify({filename: name, filedata: data}));
  window.location = "this was the Prolific URL with the completion code";
}
timeline.push(instructions,pro_id,procedure,final)

                var timestamp = Date.now();

jsPsych.init({
timeline: timeline,
on_finish: function(){ saveData(timestamp, jsPsych.data.get().csv()); }
})

Mara Haslam

unread,
Oct 1, 2020, 8:13:52 AM10/1/20
to Julie Gerard, jsPsych
Julie,
This is a wild guess, but are your participants all using the php file at the same time?  If so, it might be busy processing one person's data so that another person's data doesn't get saved.
I had an error like this in my experiments and I solved it with a web worker.
Mara

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/ffc461a4-b9b5-4978-bb05-a47b46a9d1b2n%40googlegroups.com.

Josh de Leeuw

unread,
Oct 1, 2020, 9:09:02 AM10/1/20
to Mara Haslam, Julie Gerard, jsPsych
I think this is because some participants are being redirected to prolific before the XMLHttpRequest is sent. XMLHttpRequests are asynchronous, so the window.location part of the code may execute before the request makes it to the server. 

You could put the 

window.location = "this was the Prolific URL with the completion code"; 

part of the code inside the onload callback:

xhr.onload = function() {
 window.location = "this was the Prolific URL with the completion code"; 
}

Julie Gerard

unread,
Oct 1, 2020, 7:06:42 PM10/1/20
to jsPsych
Thank you!
Yes, putting the window.location call inside the onload callback fixed the issue, 100% of data sent now.

philip....@york.ac.uk

unread,
Feb 24, 2021, 8:26:23 AM2/24/21
to jsPsych
A bit of a dull question but in my data saving function copied from jspsych


function saveData() { 
  var xhr = new XMLHttpRequest(); 
   xhr.open('POST', 'write_data.php'); // change 'write_data.php' to point to php script. 
  xhr.setRequestHeader('Content-Type', 'application/json'); 
  xhr.onload = function() { if(xhr.status == 200){ var response = JSON.parse(xhr.responseText); console.log(response.success); } }; xhr.send(jsPsych.data.get().json()); }



The onload call comes before the send call so how does it know to redirect the user only once the send has completed.

Thanks in anticipation,

Philip.

Josh de Leeuw

unread,
Feb 24, 2021, 9:18:08 AM2/24/21
to philip....@york.ac.uk, jsPsych
Hi Philip,

The xhr.onload = function() { ... } line sets up an event handler. This is just establishing what the XMLHttpRequest should do when it loads. The send() begins the execution process, which will eventually trigger the load event.

Cheers,
Josh

philip....@york.ac.uk

unread,
Feb 24, 2021, 9:19:39 AM2/24/21
to jsPsych
Thanks Josh I am new to callbacks and struggling a bit.
Best regards,
Philip.
Reply all
Reply to author
Forward
0 new messages