Wilbert's website at SocSci

> jsPsych> A1 Counting

jspsych/A1counting.html 2021-06-23

Programming in JavaScript with jsPsych, Counting participants

You have now been able to run an experiment and save your data, retrieve it and linking it to participants. You may not have noticed, but your participants are completely independent. This may be what you want. You may need a counter that generates consecute participant numbers.

participant_counter.php is a counter function that returns a unique number (starting at 0, and increasing at every new request). In the beginning of you experiment, you need to fetch a counter value. If you do not want a participant number to be reused, the counter should be finizalized. If the counter is not finalized within some period (that you can specify in the first request, timeout can be a few hours or even a few days, whatever you prefer), the counter value can be given to another participant.

ppn = -1 // as long as it does not have a value
url = "https://www.socsci.ru.nl/wilberth/nocms/radcloud/participant_counter.php"
experimentName = "test-counter"
const init = async () => {
	// get next unused participant number
	response = await fetch(url + `?experiment=${experimentName}&timeout=3600`)
	json = await response.json()
	ppn = json.counter

	// add ppn to every trial
	jsPsych.data.addProperties({ppn: ppn}) 

	// first and only trial, show the experiment name and the paerticipant number
	hello = {
		type: "html-keyboard-response",
		stimulus: `experiment name: ${experimentName}, ppn: ${ppn}`,
	}

	jsPsych.init({
		timeline: [hello],
		on_finish: async () => {
			// finalize counter
			await fetch(url + `?experiment=${experimentName}&counter=${ppn}`)
			// show data to participant (for testing only)
			jsPsych.data.displayData("json")
		},
	})
}
// run initialization after page has loaded
window.addEventListener("load", init)

Note that the whole jsPsych initialisation happens in the asynchronous function init (line 4). It is called after the whole page was loaded. (line 34). The whole initialisation has to wait for the participant number call to return (line 6) and to be parsed. This way it is ensured that the value of ppn is known when it is used for making a stimulus (line 16).

Only at the end of the experiment is the participant number finalized. This is a matter of choice. finalizing it at the end makes sure that the numer is reused if not all data is collected for this number, but may cause several incomplete datasets with the same number.

In a real experiment this participant number (ppn) can be used to choose the stimuli by using a randomized counterbalanced list.