Ending loop after specified time not working on Pavlovia

URL of experiment: https://gitlab.pavlovia.org/Mi_Lab/cba_tempdist_p2

I am attempting to create a loop that ends after 16 seconds from the onset of the loop. This runs fine in PsychoPy, but for some reason the loop will continue for longer than 16 seconds on Pavlovia. Here is a screenshot of the relevant experiment flow:

“Timer” is just a code component, in Begin Routine (JS):

startTime = new util.Clock();

and in “WS_Response”, Each Frame:

if (new util.Clock() - startTime >= 16) {
    trials.finished = true;
    continueRoutine = false; 
} else {
    trials.finished = false;
    continueRoutine = true;
}

I have tried making multiple small changes to this (such as removing the else statement) with no luck.

I also want to note that this task has been placed at the beginning of my task temporarily just so I can easily see the changes I make when piloting, but will actually be placed in the centre of my experiment flow. It is important that the timer does not start at the beginning of the experiment but at the point in which the word scramble task begins.

Thanks,
Kate

My understanding of your code is that you are creating a new clock every frame.

You already have startTime=new util.Clock(); in Begin Routine so just put startTime in your Each Frame code.

Best wishes,

Wakefield

P.S. continueRoutine=true is redundant.

Hi Wakefield,

Thank you for your suggestion :slight_smile: unfortunately I am still getting the same error, but I think I understand the logic behind what you are saying. Here is my updated code:

Begin routine:

startTime = new util.Clock();

Each frame:

if (startTime >= 16) {
    trials.finished = true;
    continueRoutine = false;
}

I think you should add a console log or text component to display startTime to check it’s working as expected.

Also, does >= auto translate to >== ?

Hi all, I think this is a server issue since any new experiments have problems with piloting or running. This includes both experiments from new repositories and forked experiments from experiments that are known to work. It only seems to affect new ones, as old experiments seem to run fine.

1 Like

Here is the console when I entered console.log(startTime), checked after the 16 seconds had passed in piloting:

Screen Shot 2020-06-24 at 3.43.36 PM

I checked and it looks like it auto translates to >=. I tried entering >== but it produced a syntax error.

I have been using Date.now() for JS for each frame. It is in milliseconds, so increase your conditionals by 1000.

I’ve changed both instances of new util.Clock() to Date.now(), which does exist the loop but consistently too early. Once the program gets to the routine that contains that code component with Each Frame, it quits.

I’m quite confused why this is happening, because the logic makes sense to me and I’ve followed directions from similar discourse posts.

I am having a similar problem today. My timer is being updated correctly, but continueRoutine is always true, despite my continueRoutine = false statement being included in the “every frame” part of the code component

Edit: solution found here: Cannot exit loop in online Pavlovia (Using JS code component). I simply changed continueRoutine = false to trials.finished = true;

1 Like

Thank you everyone for your suggestions! I realized through this post that I needed to change “startTime” to “startTime.getTime()” in the “Every Frame” tab.

1 Like

Hi there, I’ve been trying to get a time limit working on pavlovia for quite awhile and have not been successful.

Psychopy 2020.2.2, windows10, firefox

I am attempting to start a timer on the first routine, and then terminate the loop after a specified number of seconds have passed by checking the timer. The set-up I have in builder works correctly, but not so much on pavlovia.

I tried various version of these attempts(
“mainSARTBlock” is my trial loop):

  1. A more “direct” translation of py to js (eg. using globalClock & maxTime) & just change the syntax to JS, but the code seemed to be completely ignored (i.e., no errors, but trials continued after specified time limit).

  2. The code below, but removing the “new util.Clock()” in Each Frame (and instead just check that timer. getTime() is >= 8 seconds). The code appears to be “ignored” as described above in #1.

  3. The code below, but swapping out “mainSARTblock” with
    a. “trials”
    b. “currentLoop” (both as per this
    For a, I get the error “ReferenceError: trials is not defined”
    For b, I get the same “code is ignored” behavior described in #1 above.

  4. The code below, but in eachFrame JS I swapped out “(((new util.Clock() - timer.getTime()) >= 8))” for both “((mainSARTBlock.thisTrialN === 2))”, “((currentLoop.thisTrialN === 2))”, “((currentLoop.thisRepN === 2))” & “((mainSARTBlock.thisRepN === 2))” to check if the issue is how trials end in JS for psychopy version 2020.2.2 , or if the issue is my time component. Same behavior of code being “ignored” (where I am looking for the loop to terminate after 2 trials to pass that are within the first iteration of the loop), which makes me think the issue is at least with the way I’m trying to force end a trial? Given this I thought it’d work. To no avail

  5. The code below, which has the same behavior of being “ignored” as described in #1 above.

I initially had errors where “timer” wasn’t being defined when I instead used .thisN instead of .thisTrialN, which I’m no longer getting, so I’m guessing the issue is in the EachFrame tab?

In Begin Routine:
Py:

if mainSARTBlock.thisN == 0: # only on the first trial
    startTime = globalClock.getTime()
maxTime = 8

JS:

if ((mainSARTBlock.thisTrialN === 0)) {
    timer = new util.Clock();
}
maxTime = 8;

In Each Frame:
Py:

if globalClock.getTime() - startTime >= maxTime:
    mainSARTBlock.finished = True
    continueRoutine = False

JS:

if (((new util.Clock() - timer.getTime()) >= 8)) {
    mainSARTBlock.finished = true;
    continueRoutine = false;
}

Any ideas on what to change to get this working in JS would be very very appreciated! About at my wit’s end with this one. Thanks!

1 Like