PennController for IBEX › Forums › Support › End experiment if attention check fails › Reply To: End experiment if attention check fails
May 7, 2026 at 1:15 am
#11156
Participant
Yes, you can store some of the trials answers as a variable, and then have a “gate” trial in which you check the variables and if they fail you show them a message with no visible button and automatically send results within the trial (so you can check if some people did it again). Here’s the code:
PennController.ResetPrefix(null);
var attentionScore = 0;
SendResults("send_results");
Sequence(
"attn1", "attn2", "attn3", "attn4",
"gate",
"welcome", "trials",
"send_results",
"end"
);
// --- Attention trials ---
newTrial("attn1",
newText("attn1-q", "What color is the sky?").print(),
newButton("attn1-a", "Blue").print(),
newButton("attn1-b", "Green").print(),
newSelector("attn1-sel").add(getButton("attn1-a"), getButton("attn1-b")).log().wait(),
getSelector("attn1-sel").test.selected(getButton("attn1-a"))
.success(newFunction("attn1-fn", () => { attentionScore += 1; }).call())
);
newTrial("attn2",
newText("attn2-q", "How many days are in a week?").print(),
newButton("attn2-a", "7").print(),
newButton("attn2-b", "5").print(),
newSelector("attn2-sel").add(getButton("attn2-a"), getButton("attn2-b")).log().wait(),
getSelector("attn2-sel").test.selected(getButton("attn2-a"))
.success(newFunction("attn2-fn", () => { attentionScore += 1; }).call())
);
newTrial("attn3",
newText("attn3-q", "What comes after Tuesday?").print(),
newButton("attn3-a", "Wednesday").print(),
newButton("attn3-b", "Monday").print(),
newSelector("attn3-sel").add(getButton("attn3-a"), getButton("attn3-b")).log().wait(),
getSelector("attn3-sel").test.selected(getButton("attn3-a"))
.success(newFunction("attn3-fn", () => { attentionScore += 1; }).call())
);
newTrial("attn4",
newText("attn4-q", "How many sides does a triangle have?").print(),
newButton("attn4-a", "3").print(),
newButton("attn4-b", "4").print(),
newSelector("attn4-sel").add(getButton("attn4-a"), getButton("attn4-b")).log().wait(),
getSelector("attn4-sel").test.selected(getButton("attn4-a"))
.success(newFunction("attn4-fn", () => { attentionScore += 1; }).call())
);
// IMPORTANT TRIAL
newTrial("gate",
newVar("score").set(() => attentionScore),
getVar("score").test.is(v => v > 3)
.failure(
newText("fail-txt", "Sorry, you did not pass the attention check.").print(),
// hide send results comment but only here.
newFunction("silent-send", () => {
$("<style>.PennController-SendResults { visibility: hidden; height: 0; overflow: hidden; }</style>").appendTo("head");
}).call(),
SendResults(), // immediately send results
newButton().wait() // no visible button to skip or anything
)
);
// --- Experiment trials ---
newTrial("welcome",
newText("welcome-txt", "Welcome! Press start to begin.").print(),
newButton("welcome-btn", "Start").print().wait()
);
newTrial("trials",
newText("trial-txt", "This is the experiment sentence.").print(),
newButton("trial-btn", "Next").print().wait()
);
newTrial("end",
newText("end-txt", "Thank you!").print(),
newButton("end-btn", "Finish").print().wait()
);