TypeError: key_resp.keys.length

URL of experiment:

https://gitlab.pavlovia.org/hreese/test_cogload_jversion/blob/master/mitgrid_cb_try.js

Description of the problem:

Using code elements in PsychoPy, I added a visual feedback for responses (the chosen response flashes blue) and then a 0.5 delay to “force end of routine” so that participants actually see this visual feedback before proceeding to the next question.

In PsychoPy, this works fine.

However, upon being uploaded to Pavlovia, I receive the following error:

In the JS, I’ve tried changing it to:
Object.keys(key_resp).length;
but this also did not work.

Can anyone help me resolve this error?

The issue is that key_resp doesn’t exist to have a length until a key has been pressed.

You could start with if key_resp != undefined:

Hi,

Thanks very much for the response.

I tried it in the two following combinations and it did not work. With the first, the experiment started but my second routine just flashed over and over on the screen, and with the second the experiment failed to successfully initialize.

Update:

I also tried it with the correct parentheses (below) but this also doesn’t totally work. The routine does begin but after giving the first response, it does not force the end of the routine and therefore “freezes”.

How were you trying to end the routine?

Syntax errors can be reduced by writing Python code in an Auto code component, even if you then have to switch it to Both for the finishing touches.

I’m using the code in the original post to end the routine.

if ((feedback_given && (t > (endTime + 0.5)))) {
continueRoutine = false;
}

Please could you show your whole Each Frame Python code? I can’t tell how / if you’ve embedded the new code into your previous feedback_given code.

I was actually making the changed directly into the JS code in Github, but now I’ve gone back to making the changes in PsychoPy. Here’s what my Each Frame looks like now:

It’s still working but not going on to the next question (in PsychoPy I had it so that it would force end of routine with a .5s delay).

I should also mention that I have the following in the Begin Routine frame as well:

I’m not sure how I should substitute “feedback_given” …

Thanks again for your help.

That suggests that you haven’t come across my crib sheet.

Specifically in this case, I’d recommend that you use colours as variables.

Also, you may as well use:

elif t > endTime+.5:

instead of if key_resp != undefined and t > endTime+.5:

I had not. Thanks, I won’t edit in GitHub again :wink:

I can use the colors as variables, but as the color is now successfully changing I don’t think that is the main issue. The issue now is that the routine is not ending.

I tried
elif t > endTime+.5:

but that also didn’t work.

Should I be replacing
feedback_given = True
with something else?

Please could you copy and paste your Python code so I can easily edit it to make a suggestion.

if key_resp != undefined:

if key_resp.keys == '1':
    links.color = 'blue'
elif key_resp.keys == '0':
    rights.color = 'blue'
    
feedback_given = True # so we don't keep repeating this step
endTime = t

elif t > endTime+.5:
continueRoutine=False

The following code tries to avoid the issue with undefined only working online. If it fails then just change the first line to if feedback_given == False and key_resp != undefined:

if feedback_given == False:
     if key_resp.keys == '1':
          links.color = 'blue'
          feedback_given = True
          endTime = t+.5
     elif key_resp.keys == '0':
          rights.color = 'blue'
          feedback_given = True
          endTime = t+.5    
elif t > endTime:
     continueRoutine=False

It works! Thanks a million!