diff --git a/examples/jspsych-categorize-animation.html b/examples/jspsych-categorize-animation.html index f488ba1f..507625d0 100644 --- a/examples/jspsych-categorize-animation.html +++ b/examples/jspsych-categorize-animation.html @@ -1,77 +1,183 @@ - - - - jspsych-categorize-animation plugin example - - - - - - - - - - - -
- - - \ No newline at end of file + if (!responded && trial.allow_response_before_complete) { + // in here if the user can respond before the animation is done + if (trial.prompt !== "") { + display_element.append(trial.prompt); + } + } + else if (!responded) { + // in here if the user has to wait to respond until animation is done. + // if this is the case, don't show the prompt until the animation is over. + if (!showAnimation) { + if (trial.prompt !== "") { + display_element.append(trial.prompt); + } + } + } + else { + // user has responded if we get here. + + // show feedback + var feedback_text = ""; + if (block.data[block.trial_idx].correct) { + feedback_text = trial.correct_text.replace("%ANS%", trial.text_answer); + } + else { + feedback_text = trial.incorrect_text.replace("%ANS%", trial.text_answer); + } + display_element.append(feedback_text); + + // set timeout to clear feedback + if (!timeoutSet) { + timeoutSet = true; + setTimeout(function() { + endTrial(); + }, trial.timing_feedback_duration); + } + } + + + }, trial.frame_time); + + // attach response function + + var resp_func = function(e) { + + if (!trial.allow_response_before_complete && showAnimation) { + return false; + } + + var flag = false; // valid keystroke? + var correct = false; // correct answer? + + if (e.which == trial.key_answer) // correct category + { + flag = true; + correct = true; + } + else { + // check if the key is any of the options, or if it is an accidental keystroke + for (var i = 0; i < trial.choices.length; i++) { + if (e.which == trial.choices[i]) { + flag = true; + correct = false; + } + } + } + if (flag) // if keystroke is one of the choices + { + responded = true; + var endTime = (new Date()).getTime(); + var rt = (endTime - startTime); + + var trial_data = { + "trial_type": trial.type, + "trial_index": block.trial_idx, + "stimulus": trial.stims[0], + "rt": rt, + "correct": correct, + "key_press": e.which + }; + block.writeData($.extend({}, trial_data, trial.data)); + $(document).unbind('keyup', resp_func); + } + }; + $(document).keyup(resp_func); + + function endTrial() { + clearInterval(animate_interval); // stop animation! + display_element.html(''); // clear everything + setTimeout(function() { + block.next(); + }, trial.timing_post_trial); + } + }; + + return plugin; + })(); +})(jQuery); \ No newline at end of file