CoreDump: An Experiment in Peace.

var trial = { type: jsPsychImageKeyboardResponse, stimulus: ‘##Fill the Image Link##’, choices: [‘e’, ‘i’], prompt: “<p>Type Your Response? Press ‘e’ for happy and ‘i’ for sad.</p>”, };
<!DOCTYPE html> 
<html>
<head>
<title>Peace!</title>
<script src="https://unpkg.com/jspsych@7.0.0"></script>
<script src="https://unpkg.com/@jspsych/plugin-html-keyboard-response@1.0.0"></script>
<script src="https://unpkg.com/@jspsych/plugin-image-keyboard-response@1.0.0"></script>
<script src="https://unpkg.com/@jspsych/plugin-preload@1.0.0"></script>
<link href="https://unpkg.com/jspsych@7.0.0/css/jspsych.css" rel="stylesheet" type="text/css" />
</head> <body></body>
<script> /* initialize jsPsych */
var jsPsych = initJsPsych({ on_finish: function() { jsPsych.data.displayData();
}
});
/* create timeline */
var timeline = []; /* preload images */
var preload = {
type: jsPsychPreload,
images: ['https//unsplash.com/photos/WFRBQ94Xhhc', https//unsplash.com/photos/xnK_o6OdTew',
https//unsplash.com/photos/-lWubJa7co, https://unsplash.com/photos/PEJHULxUHZs,
https://unsplash.com/photos/mhib6U1gMcU] }; timeline.push(preload); /* define welcome message trial */ var welcome = {
type: jsPsychHtmlKeyboardResponse,
stimulus: "Welcome to the experiment. Press any key to begin." };
timeline.push(welcome);
/* define instructions trial */
var instructions = {
type: jsPsychHtmlKeyboardResponse,
stimulus: `
<p>In this experiment, five images appear in the center of the screen.</p>
<p>If you sense war </strong>,
press the letter W on the keyboard as fast as you can.</p> <p>if you sense peace <strong>orange</strong>,
press the letter P
as fast as you can.</p>
<div style='width: 700px;'>
<div style='float: left;'>
<img src='img/blue.png'></img>
<p class='small'><strong>Press the F key</strong></p>
</div>
<div style='float: right;'><img src='img/orange.png'></img> <p class='small'><strong>Press the J key</strong></p></div> </div>
<p>Press any key to begin.</p> `, post_trial_gap: 2000 }; timeline.push(instructions); /* define trial stimuli array for timeline variables */
var test_stimuli = [
{
stimulus: "https://unsplash.com/photos/mhib6U1gMcU", correct_response: 'w'},
{
stimulus: "https://unsplash.com/photos/PEJHULxUHZs", correct_response: 'w'},
stimulus: "https://unsplash.com/photos/-lWubJa7-co", correct_response: 'w'},
stimulus: "https://unsplash.com/photos/xnK_o6OdTew", correct_response: 'w'},
stimulus: "https://unsplash.com/photos/WFRBQ94Xhhc", correct_response: 'w'}
]; /* define fixation and test trials */ var fixation = {
type: jsPsychHtmlKeyboardResponse, stimulus: '
<div style="font-size:60px;">+</div>', choices: "NO_KEYS", trial_duration:
function(){
return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0]; }, data: { task: 'fixation' } };
var test = { type: jsPsychImageKeyboardResponse, stimulus: jsPsych.timelineVariable('stimulus'), choices: ['f', 'j'],
data: { task: 'response', correct_response: jsPsych.timelineVariable('correct_response') }, on_finish: function(data){
data.correct = jsPsych.pluginAPI.compareKeys(data.response, data.correct_response); } };
/* define test procedure */
var test_procedure = { timeline: [fixation, test], timeline_variables: test_stimuli, repetitions: 5, randomize_order: true };
timeline.push(test_procedure); /* define debrief */ var debrief_block = {
type: jsPsychHtmlKeyboardResponse, stimulus: function() { var trials = jsPsych.data.get().filter({task: 'response'}); var correct_trials = trials.filter({correct: true}); var accuracy = Math.round(correct_trials.count() / trials.count() * 100); var rt = Math.round(correct_trials.select('rt').mean()); return `<p>You responded correctly on ${accuracy}% of the trials.</p>
<p>Your average response time was ${rt}ms.</p>
<p>Press any key to complete the experiment. Thank you!</p>`; } };
timeline.push(debrief_block); /* start the experiment */ jsPsych.run(timeline);
//data persistence
var data = jsPsych.data.get().filter({trial_type: 'image-keyboard-response'});
var count = jsPsych.data.get().filter({correct: true}).count();
var response_times = jsPsych.data.get().select('rt');
jsPsych.data.get().select('rt').mean(); jsPsych.data.get().select('rt').sum(); jsPsych.data.get().select('rt').min(); jsPsych.data.get().select('rt').max(); jsPsych.data.get().select('rt').variance(); jsPsych.data.get().select('rt').sd(); jsPsych.data.get().select('rt').median(); jsPsych.data.get().select('rt').count();
<?php // get the data from the POST message
$post_data = json_decode(file_get_contents('php://input'), true); $data = $post_data['filedata']; // generate a unique ID for the file, e.g., session-6feu833950202
$file = uniqid("session-"); // the directory "data" must be writable by the server
$name = "data/{$file}.csv"; // write the file to disk file_put_contents($name, $data); ?>
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({filedata: data})); } // call the saveData function after the experiment is over
initJsPsych({ on_finish: function(){ saveData(jsPsych.data.get().csv()); }
});
var trial = { type: jsPsychCallFunction, async: true, func: function(done){
var xhr = new XMLHttpRequest();
xhr.open('POST', 'write_data.php'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onload = function() {
if(xhr.status == 200){
var response = JSON.parse(xhr.responseText); console.log(response.success); }
done(); // invoking done() causes experiment to progress to next trial. };
xhr.send(jsPsych.data.get().json()); } }

</script>
</html>
<?php // get the data from the POST message 
$post_data = json_decode(file_get_contents('php://input'), true); $data = $post_data['filedata']; // generate a unique ID for the file, e.g., session-6feu833950202
$file = uniqid("session-"); // the directory "data" must be writable by the server
$name = "data/{$file}.csv"; // write the file to disk file_put_contents($name, $data); ?>

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Begineer JavaScript Developers have to know these facts

Creating a multilingual application with Ngx-Translate: a simple approach

Building a Website Chatbot using Dialogflow and Quickwork

Form submission — why it’s crucial to use a button of submit type

Building a Snake Game using Canvas

Installing Strapi and Using MongoDB Atlas as Default Database

Deep Link Routing to Your Android or Apple App or App Store in Vue/Nuxt

Using Styled-Components In React.JS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anil Kumar B

Anil Kumar B

More from Medium

5 Reasons Quobyte Storage Enables Stronger Cryo-EM Solutions

The Tipsy Pig — Tekamah, NE

CoinRadr

Z-Invest