Cross randomly appears between trials

URL of experiment:

Description of the problem:
Hello!

I’ve created an online experiment in which (in the first session) participants will see 48 shapes paired with 48 sounds. As they are only asked to memorise each pair, I want to see if they are paying attention during the presentation of the shapes-sounds. Therefore, I’d like to have a red cross that appears on the screen and participants are asked to press the space bar every time this occurs. Specifically, I want this red cross to randomly appear between trials 5 to 8 / trials 15 to 18 / and between trials 25 to 28.

I know I need to do it with the code component, can anyone help me to do that in JS?

Thank you!!

You need a loop counter

e.g. trialNum = -1 in Begin Experiment

You could create the random points using randint (see my crib sheet for how to define randint)

stop1 = randint(4,8)
stop2 = randint(14,18)
stop3 = randint(24,28)

Then add a routine for your cross and in Begin Routine put

trialNum+=1
if trialNum != stop1 and trialNum != stop2 trialNum != stop3
     continueRoutine=False

Hi @wakecarter ,

Thank you very much for your quick reply!
I’ve just updated the code as you suggested, but the experiment in stuck on “Initialising the experiment”.

Attached some screenshot to look at what I did



Defining randint needs to be an a JS code component (which I call code_JS).

The other code is Python and goes in an Auto component.

However, I forgot the colon : at the end of the Begin Routine if statement.

Thank you @wakecarter

It is working now, however, the cross appears after every single trial.
Do you have an idea of why?

After the code component, I have a polygon and a keyboard component. Both components have no duration as I want participants to press the space bar before moving on the next trials. (Keyboard component is set to “Force end of Routine”).


As per my crib sheet you shouldn’t use trials as a variable name.

Oh, sorry, didn’t notice @wakecarter

Unfortunately, after changing trials to trialNum, the cross still appear at every trial on the screen

What version of PsychoPy are you using?

In some versions you can’t use continueRoutine=False in Begin Experiment so you could put the if statement in Each Frame

Thanks! it works perfectly now!!

Hi @wakecarter

Sorry if I go back to this. The cross is working fine, but when I set my loop to repeat for three times, the cross randomly appear only in the first repetition, and not in the other 2.
Do you know why?

Thank you

Do you perhaps need to reset the loop counter?

Sorry, What do you mean by resetting the counter?

Something like that?
if ((trialNum === 48)) {
trialNum = 1;
} else {
trialNum += 1;
}

Yes, something like that should do.