Continue routine on space does not work as intended

Hello,
I have a very simple code that intends to just continue the routine when clicking the spacebar if more than a second has passed. Unfortunately, if I click the spacebar multiple times the trials just skip themselves.
In the Each Frame tab, the code is as follows:

if (t>1 && (spaceclicked === false) && psychoJS.eventManager.getKeys({keyList:['space']}).length > 0) {
    spaceclicked = true;
    timer = new util.Clock();
    timer.add((1+effectDelay));
    while ((timer.getTime() < 0)) {
        continueRoutine = false;
    }
}

I tried to solve it by setting spaceclicked to false on each “Begin Routine” and setting it to true after space was clicked.
Help will be much appreciated.

Don’t use while in Builder. I would also recommend using Auto translated code.

Here is some simpler code:

Begin Routine

event.clearEvents()
spaceClicked=0

Each Frame

if 'space' in event.getKeys():
     spaceClicked=t
elif t > spaceClicked + 1:
     continueRoutine=False

I’m not sure I quite understood what you wanted. This should require the space bar to be pressed at least once per second (but at the moment it could just be held down online).

Please see my crib sheet for how to pre-define things like event so you can use Auto translate components as much as possible.

Thank you for your comment,
my intention is that the subject will press the spacebar once, and the trial will end. But, I want that to work only if one second has passed since the beginning of the trial.

I wrote code for that first but then decided that you probably wanted something more complicated.

Begin Routine

event.clearEvents()

Each Frame

if 'space' in event.getKeys() and t > 1:
     continueRoutine=False

Thank you again for your reply,
I get the following error message:

  • TypeError: Cannot read property ‘clearEvents’ of undefined
    I used auto-translate code.
    I also forgot to mention that after the subject hits the spacebar, the trial should end only a second after it.

You need to put event=psychoJS.eventManager; in code_JS or incorporate PsychoPolyfill.js as per my crib sheet (or manually translate event on the JS side only for every occurrence).

If you want it to end one second after the spacebar has been pressed then you need.

Begin Routine

event.clearEvents()
spaceClicked=999

Each Frame

if 'space' in event.getKeys() and t > 1:
     spaceClicked=t
elif t > spaceClicked+1:
     continueRoutine=False
1 Like