mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-10 19:20:55 +00:00
implement pause/unpause #232
This commit is contained in:
parent
ecbdfa50dd
commit
01b78b54b9
76
jspsych.js
76
jspsych.js
@ -24,6 +24,9 @@ var jsPsych = (function() {
|
||||
var DOM_target;
|
||||
// time that the experiment began
|
||||
var exp_start_time;
|
||||
// is the experiment paused?
|
||||
var paused = false;
|
||||
var waiting = false;
|
||||
|
||||
//
|
||||
// public methods
|
||||
@ -133,37 +136,16 @@ var jsPsych = (function() {
|
||||
if (opts.default_iti > 0) {
|
||||
setTimeout(next_trial, opts.default_iti);
|
||||
} else {
|
||||
next_trial();
|
||||
nextTrial();
|
||||
}
|
||||
} else {
|
||||
if (current_trial.timing_post_trial > 0) {
|
||||
setTimeout(next_trial, current_trial.timing_post_trial);
|
||||
} else {
|
||||
next_trial();
|
||||
nextTrial();
|
||||
}
|
||||
}
|
||||
|
||||
function next_trial() {
|
||||
global_trial_index++;
|
||||
|
||||
// advance timeline
|
||||
timeline.markCurrentTrialComplete();
|
||||
var complete = timeline.advance();
|
||||
|
||||
// update progress bar if shown
|
||||
if (opts.show_progress_bar === true) {
|
||||
updateProgressBar();
|
||||
}
|
||||
|
||||
// check if experiment is over
|
||||
if (complete) {
|
||||
finishExperiment();
|
||||
return;
|
||||
}
|
||||
|
||||
doTrial(timeline.trial());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
core.endExperiment = function(end_message) {
|
||||
timeline.end_message = end_message;
|
||||
@ -190,8 +172,25 @@ var jsPsych = (function() {
|
||||
return timeline.timelineVariable(varname);
|
||||
}
|
||||
|
||||
core.addNodeToEndOfTimeline = function(new_timeline){
|
||||
core.addNodeToEndOfTimeline = function(new_timeline, preload_callback){
|
||||
timeline.insert(new_timeline);
|
||||
if(opts.auto_preload){
|
||||
jsPsych.pluginAPI.autoPreload(new_timeline, preload_callback);
|
||||
} else {
|
||||
preload_callback();
|
||||
}
|
||||
}
|
||||
|
||||
core.pauseExperiment = function(){
|
||||
paused = true;
|
||||
}
|
||||
|
||||
core.resumeExperiment = function(){
|
||||
paused = false;
|
||||
if(waiting){
|
||||
waiting = false;
|
||||
nextTrial();
|
||||
}
|
||||
}
|
||||
|
||||
function TimelineNode(parameters, parent, relativeID) {
|
||||
@ -657,6 +656,33 @@ var jsPsych = (function() {
|
||||
|
||||
}
|
||||
|
||||
function nextTrial() {
|
||||
// if experiment is paused, don't do anything.
|
||||
if(paused) {
|
||||
waiting = true;
|
||||
return;
|
||||
}
|
||||
|
||||
global_trial_index++;
|
||||
|
||||
// advance timeline
|
||||
timeline.markCurrentTrialComplete();
|
||||
var complete = timeline.advance();
|
||||
|
||||
// update progress bar if shown
|
||||
if (opts.show_progress_bar === true) {
|
||||
updateProgressBar();
|
||||
}
|
||||
|
||||
// check if experiment is over
|
||||
if (complete) {
|
||||
finishExperiment();
|
||||
return;
|
||||
}
|
||||
|
||||
doTrial(timeline.trial());
|
||||
}
|
||||
|
||||
function doTrial(trial) {
|
||||
|
||||
current_trial = trial;
|
||||
|
36
tests&examples/pause-unpause.html
Normal file
36
tests&examples/pause-unpause.html
Normal file
@ -0,0 +1,36 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="js/jquery.min.js"></script>
|
||||
<script src="../jspsych.js"></script>
|
||||
<script src="../plugins/jspsych-text.js"></script>
|
||||
<link rel="stylesheet" href="../css/jspsych.css"></link>
|
||||
</head>
|
||||
<body>
|
||||
<div id="jspsych-target"></div>
|
||||
</body>
|
||||
<script>
|
||||
|
||||
var first = {
|
||||
type: 'text',
|
||||
text: 'first trial!',
|
||||
timing_post_trial: 0,
|
||||
on_finish: function(){
|
||||
jsPsych.pauseExperiment();
|
||||
setTimeout(jsPsych.resumeExperiment, 4000);
|
||||
}
|
||||
}
|
||||
|
||||
var second = {
|
||||
type: 'text',
|
||||
text: 'second trial!'
|
||||
}
|
||||
|
||||
jsPsych.init({
|
||||
display_element: $('#jspsych-target'),
|
||||
timeline: [first, second],
|
||||
on_finish: function(){jsPsych.data.displayData(); }
|
||||
});
|
||||
|
||||
</script>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user