jsPsych/examples/demo-flanker.html
2016-12-09 21:25:56 -05:00

128 lines
4.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Flanker Task</title>
<script src="../jspsych.js"></script>
<script src="../plugins/jspsych-text.js"></script>
<script src="../plugins/jspsych-single-stim.js"></script>
<link rel="stylesheet" href="../css/jspsych.css">
</head>
<body>
</body>
<script>
/*set up welcome block*/
var welcome = {
type: "text",
text: "Welcome to the experiment. Press any key to begin."
};
/*set up instructions block*/
var instructions = {
type: "text",
text: "<p>In this task, you will see five arrows on the screen.</p>"+
"<p>Press the left arrow key if the middle arrow is pointing left. (<)</p>"+
"<p>Press the right arrow key if the middle arrow is pointing right. (>)</p>"+
"<p>Press any key to begin.</p>",
timing_post_trial: 3000
};
/*defining stimuli*/
var test_stimuli = [
{
stimulus: "img/con1.png",
data: { stim_type: 'congruent', direction: 'left'}
},
{
stimulus: "img/con2.png",
data: { stim_type: 'congruent', direction: 'right'}
},
{
stimulus: "img/inc1.png",
data: { stim_type: 'incongruent', direction: 'right'}
},
{
stimulus: "img/inc2.png",
data: { stim_type: 'incongruent', direction: 'left'}
}
];
/*randomising stimuli*/
var all_trials = jsPsych.randomization.repeat(test_stimuli, 3);
/*creating random ISI*/
var post_trial_gap = function() {
return Math.floor(Math.random() * 1500) + 500;
};
/*defining experimental block*/
var test_block = {
type: 'single-stim',
choices: [37, 39],
timing_response: 1500,
on_finish: function(data){
var correct = false;
if(data.direction == 'left' && data.key_press == '37' && data.rt > -1){
correct = true;
} else if(data.direction == 'right' && data.key_press == '39' && data.rt > -1){
correct = true;
}
jsPsych.data.addDataToLastTrial({correct: correct});
},
timeline: all_trials,
timing_post_trial: post_trial_gap
};
/*function for getting mean RTs and error rates*/
function getSubjectData() {
var trials = jsPsych.data.getData({trial_type: 'single-stim'});
var congruent_trials = jsPsych.data.getData({stim_type: 'congruent', correct: true});
var incongruent_trials = jsPsych.data.getData({stim_type: 'incongruent', correct: true});
var sum_rt_congruent = 0;
for (var i = 0; i < congruent_trials.length; i++) {
sum_rt_congruent += congruent_trials[i].rt;
}
var sum_rt_incongruent = 0;
for (var i = 0; i < incongruent_trials.length; i++) {
sum_rt_incongruent += incongruent_trials[i].rt;
}
return {
congruent_rt: Math.floor(sum_rt_congruent / congruent_trials.length),
incongruent_rt: Math.floor(sum_rt_incongruent / incongruent_trials.length),
accuracy: Math.floor( (congruent_trials.length + incongruent_trials.length) / trials.length * 100)
}
};
/*defining debriefing block*/
var debrief_block = {
type: "text",
text: function() {
var subject_data = getSubjectData();
return "<p>You responded correctly on "+subject_data.accuracy+"% of the trials.</p> " +
"<p>Your average response time for congruent trials was <strong>" + subject_data.congruent_rt + "ms</strong>.</p>"+
"<p>Your average response time for incongruent trials was <strong>" + subject_data.incongruent_rt + "ms</strong>.</p>"+
"<p>Press any key to complete the experiment. Thank you!</p>";
}
};
/*set up experiment structure*/
var timeline = [];
timeline.push(welcome);
timeline.push(instructions);
timeline.push(test_block);
timeline.push(debrief_block);
/*start experiment*/
jsPsych.init({
timeline: timeline,
on_finish: function() {
jsPsych.data.displayData();
}
});
</script>
</html>