psychopy.org | Reference | Downloads | Github

Translating problems python commands to JS

I have had a python command added in the custom code section on the builder view to add an random interval between sound trigger and stimulus onset (image)

e.g. code_5

waitTime = randint(2, 8)
waitTime = waitTime / 10
#print(waitTime)

waitClock = core.CountdownTimer()
waitClock.reset()
waitClock.add(1.650000)

-------Run Randon time Wait routine"-------

while waitClock.getTime() > 0:
if defaultKeyboard.getKeys(keyList=[“escape”]):
core.quit()

However, someone told me that 'the CountDownTimer() , in python there is module named core and from which we can access the CountDownTimer() method,
and use it in our project easily as it is already used in your “.py” file,but for same thing to be done in javascript there was no module available over internet, cause there was no direct library in javacript and that there does not exist any libraries that could function the same as they could in python.

Link to study is: https://run.pavlovia.org/smoha005/sfm_tffm/html/

Hi @smoha005, the equivalent is available in the PsychoJS library. E.g.,

myTimer = new util.CountdownTimer();
myTimer.reset()

There is also a syntax error in your code: Wait Time in your code_4 component.

I have checked the PsychoJS Library but I can’t seem to find the equivalent. Could you please direct me to where I could find it. Thank you.

The code that I am trying to convert is the following, I’ve been told it will be very difficult to translate to Javascript

waitTime = waitTime / 10
#print(waitTime)

waitClock = core.CountdownTimer()
waitClock.reset()
waitClock.add(1.650000)

# -------Run Randon time Wait routine"-------
while waitClock.getTime() > 0:
    if defaultKeyboard.getKeys(keyList=["escape"]):
        core.quit()```

It might be better if you describe what you are trying to achieve , because there is a good chance you can do what you want without much coding, using Builder. JavaScript is not much different to Python, e.g.,:

myTimer = new util.CountdownTimer();
myTimer.reset();
myTimer.add(1.65000);

if (myTimer.getTime > 0) 
  {  
    let myKeyPress = defaultKeyboard.getKeys(keyList=['escape'])
    if ( myKeyPress.length > 0 )
      {
        return quitPsychoJS('The [Escape] key was pressed. Goodbye!', false);
      }
  }

Also, in the next release of PsychoPy (2020.1) due to be released any day now, there is an autotranslate function in the code components for helping with code translation in your code components.

I am trying to add a random interval between 100 - 400ms between the sound and the stimulus onset. I haven’t found a way to do it using the builder so I was advised to code it

If you are only playing a sound, and then showing a visual stimulus, then your easiest solution is to separate your sound and stimulus into separate routines in Builder. Between the sound and the stimulus routine, add a new routine for your jitter. To the new jitter routine, you could add a blank text component, or something else, like a static component, and set the duration as code (e.g., $jitter) and define ‘jitter’ in a code component in the new routine.

// Begin Routine
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}

jitter = getRandomIntInclusive(100, 400)  // Gives random value between 100 and 400, inclusive
// This value can be passed to your blank component for providing
// a random gap between sound and stimulus

Thank you so much for your help.

I have tried what you have suggested above however the when I upload it on Pavlovia it I am just presented with a blank screen for a long time where the blank text component is. I have set the duration as $jitter and put the above code into the custom code.

Link to study: https://run.pavlovia.org/smoha005/tester101/html/

@smoha005, apologies, as PsychoPy uses time in seconds, not milliseconds, we have random times between 100 and 400 seconds. To fix, just convert the ms to seconds using $jitter / 1000

Thank you so much for your help. It’s working now.