Continuously looping a trial for a predetermined duration #1408
-
I am designing an experiment in which participants must perform simple arithmetic for 30 seconds before going on to the next task. I initially tried to do this by setting a timer and using a var thereistime = true;
while(thereistime) {
Int1 = getRndInteger(-2, 3);
Int2 = getRndInteger(1, 3);
Int3 = getRndInteger(1, 3);
var math_distractor = {
type: "survey-text",
questions: [{prompt: '<p>' + Int1 + ' + ' + Int2 + ' + ' +
Int3 + ' = ?<p>', required: true}]
};
timeline.push(math_distractor);
};
setTimeout(function(){thereistime = false}, 30000); When that didn't work, I also tried using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @awb99cu, your approach is totally sensible from a 'normal' programming perspective. The reason this doesn't work is that jsPsych needs to construct the timeline before the experiment starts, so it's not really possible to add new trials onto the Also, note that you'll also need to use a dynamic parameter for the survey-text Here's one method that might work for you - though it might not be the most elegant solution! var started = false;
var start_time;
// create your math trial
var math_distractor = {
type: "survey-text",
questions: function() {
// if this is the first math trial, then record the start time
// (or another option is to record the start time at the end of the trial that comes before this in your timeline)
if (!started) {
started = true;
start_time = performance.now();
}
var Int1 = getRndInteger(-2, 3);
var Int2 = getRndInteger(1, 3);
var Int3 = getRndInteger(1, 3);
var prompt = '<p>' + Int1 + ' + ' + Int2 + ' + ' + Int3 + ' = ?<p>';
return [{prompt: prompt, required: true}];
}
};
// create the loop node that will repeat this trial until the 30 seconds is up
var math_loop = {
timeline: [math_distractor],
loop_function: function() {
var curr_time = performance.now();
if (curr_time - start_time >= 30000) {
// stop the math trial loop
return false;
} else {
// run another math trial
return true;
}
}
};
timeline.push(math_loop) |
Beta Was this translation helpful? Give feedback.
-
Thank you so much Becky! Your solution worked beautifully.
Best,
Adam
…On Fri, Jan 22, 2021 at 1:23 PM Becky Gilbert ***@***.***> wrote:
Hi @awb99cu <https://github.com/awb99cu>, your approach is totally
sensible from a 'normal' programming perspective. The reason this doesn't
work is that jsPsych needs to construct the timeline before the experiment
starts, so it's not really possible to add new trials onto the timeline
array in the way that you've been trying to do here. However, jsPsych
provides other ways to change the flow during the experiment, for instance
with conditional nodes
<https://www.jspsych.org/overview/timeline/#conditional-timelines> and looping
nodes <https://www.jspsych.org/overview/timeline/#looping-timelines>. I
think a looping node would work for what you're trying to do.
Also, note that you'll also need to use a dynamic parameter
<https://www.jspsych.org/overview/trial/#dynamic-parameters> for the
survey-text questions parameter, since this text is determined *during*
the experiment.
Here's one method that might work for you - though it might not be the
most elegant solution!
var started = false;var start_time;
// create your math trialvar math_distractor = {
type: "survey-text",
questions: function() {
// if this is the first math trial, then record the start time
// (or another option is to record the start time at the end of the trial that comes before this in your timeline)
if (!started) {
started = true;
start_time = performance.now();
}
var Int1 = getRndInteger(-2, 3);
var Int2 = getRndInteger(1, 3);
var Int3 = getRndInteger(1, 3);
var prompt = '<p>' + Int1 + ' + ' + Int2 + ' + ' + Int3 + ' = ?<p>';
return [{prompt: prompt, required: true}];
}};
// create the loop node that will repeat this trial until the 30 seconds is upvar math_loop = {
timeline: [math_distractor],
loop_function: function() {
var curr_time = performance.now();
if (curr_time - start_time >= 30000) {
// stop the math trial loop
return false;
} else {
// run another math trial
return true;
}
}};timeline.push(math_loop)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1399 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ASRTVKWUKDGFRRQLTFA4PULS3G7BDANCNFSM4WNNS5FQ>
.
--
Ph.D. Candidate
Psychology & Cognitive Science
Cornell University | Uris Hall B107
|
Beta Was this translation helpful? Give feedback.
Hi @awb99cu, your approach is totally sensible from a 'normal' programming perspective. The reason this doesn't work is that jsPsych needs to construct the timeline before the experiment starts, so it's not really possible to add new trials onto the
timeline
array in the way that you've been trying to do here. However, jsPsych provides other ways to change the flow during the experiment, for instance with conditional nodes and looping nodes. I think a looping node would work for what you're trying to do.Also, note that you'll also need to use a dynamic parameter for the survey-text
questions
parameter, since this text is determined during the experiment.Here's one method that might work…