mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-12 16:48:12 +00:00
Fixed bugs when timing_response was > 0
Related to Issue #62. Also, explicitly canceled keyboard listener in cases where trial ended and participant didn't respond.
This commit is contained in:
parent
3e96600423
commit
03ce248d0b
@ -47,9 +47,12 @@
|
|||||||
// this evaluates the function and replaces
|
// this evaluates the function and replaces
|
||||||
// it with the output of the function
|
// it with the output of the function
|
||||||
trial = jsPsych.pluginAPI.normalizeTrialVariables(trial);
|
trial = jsPsych.pluginAPI.normalizeTrialVariables(trial);
|
||||||
|
|
||||||
|
// this array holds handlers from setTimeout calls
|
||||||
|
// that need to be cleared if the trial ends early
|
||||||
|
var setTimeoutHandlers = [];
|
||||||
|
|
||||||
var trial_complete = false;
|
// display stimulus
|
||||||
|
|
||||||
if (!trial.is_html) {
|
if (!trial.is_html) {
|
||||||
display_element.append($('<img>', {
|
display_element.append($('<img>', {
|
||||||
src: trial.a_path,
|
src: trial.a_path,
|
||||||
@ -63,15 +66,23 @@
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
//show prompt here
|
//show prompt if there is one
|
||||||
if (trial.prompt !== "") {
|
if (trial.prompt !== "") {
|
||||||
display_element.append(trial.prompt);
|
display_element.append(trial.prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// function to end trial when it is time
|
||||||
var end_trial = function(info) {
|
var end_trial = function(info) {
|
||||||
|
|
||||||
|
// kill any remaining setTimeout handlers
|
||||||
|
for(var i = 0; i < setTimeoutHandlers.length; i++){
|
||||||
|
clearTimeout(setTimeoutHandlers[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// kill keyboard listeners
|
||||||
|
jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
|
||||||
|
|
||||||
trial_complete = true;
|
// gather the data to store for the trial
|
||||||
|
|
||||||
var trial_data = {
|
var trial_data = {
|
||||||
"trial_type": "single-stim",
|
"trial_type": "single-stim",
|
||||||
"trial_index": block.trial_idx,
|
"trial_index": block.trial_idx,
|
||||||
@ -82,7 +93,10 @@
|
|||||||
|
|
||||||
block.writeData($.extend({}, trial_data, trial.data));
|
block.writeData($.extend({}, trial_data, trial.data));
|
||||||
|
|
||||||
|
// clear the display
|
||||||
display_element.html('');
|
display_element.html('');
|
||||||
|
|
||||||
|
// move on to the next trial
|
||||||
if (trial.timing_post_trial > 0) {
|
if (trial.timing_post_trial > 0) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
block.next();
|
block.next();
|
||||||
@ -93,6 +107,7 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// function to handle responses by the subject
|
||||||
var after_response = function(info) {
|
var after_response = function(info) {
|
||||||
|
|
||||||
// after a valid response, the stimulus will have the CSS class 'responded'
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
||||||
@ -107,30 +122,27 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// start the response listener
|
||||||
jsPsych.pluginAPI.getKeyboardResponse(after_response, trial.choices);
|
var keyboardListener = jsPsych.pluginAPI.getKeyboardResponse(after_response, trial.choices);
|
||||||
|
|
||||||
// hide image if timing is set
|
// hide image if timing is set
|
||||||
if (trial.timing_stim > 0) {
|
if (trial.timing_stim > 0) {
|
||||||
setTimeout(function() {
|
var t1 = setTimeout(function() {
|
||||||
if (!trial_complete) {
|
$('#jspsych-single-stim-stimulus').css('visibility', 'hidden');
|
||||||
$('#jspsych-single-stim-stimulus').css('visibility', 'hidden');
|
|
||||||
}
|
|
||||||
}, trial.timing_stim);
|
}, trial.timing_stim);
|
||||||
|
setTimeoutHandlers.push(t1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// end trial if time limit is set
|
// end trial if time limit is set
|
||||||
if (trial.timing_response > 0) {
|
if (trial.timing_response > 0) {
|
||||||
setTimeout(function() {
|
var t2 = setTimeout(function() {
|
||||||
if (!trial_complete) {
|
end_trial({rt: -1, key: -1});
|
||||||
end_trial({rt: -1, key: -1});
|
|
||||||
}
|
|
||||||
}, trial.timing_response);
|
}, trial.timing_response);
|
||||||
|
setTimeoutHandlers.push(t2);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
return plugin;
|
return plugin;
|
||||||
})();
|
})();
|
||||||
})(jQuery);
|
})(jQuery);
|
||||||
|
Loading…
Reference in New Issue
Block a user