psychopy.org | Reference | Downloads | Github

Equivalent code: core.wait() for javascript

Hi,

in the experiment the subject produces circles by holding the space bar as u can see below:

when i run the code in the offline lab, it takes about 6 sec to fill up the hole big circles with dots, but when i run the code online, it takes 2 sec,

To maintain equality in times I wanted to use Python’s core.wait code only in javascript.

I saw topics that suggested using: setTimeout, setInterval.

My question is how to infuse this function into my code? This is the code in each frame tab that produces the circles and displays them:

//show the circles on the screen
for (var circle, _pj_c = 0, _pj_a = Circle_List, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    circle = _pj_a[_pj_c];
    circle.setAutoDraw(true);
}


// If any key events are present
let keyEvents = kb.getEvents();
if (keyEvents.length > 0) {
    if (keyEvents[keyEvents.length-1].pigletKey === 'space' && keyEvents[keyEvents.length-1].status === Symbol.for('KEY_DOWN')) {
        switched = false;
        if (space_func ) {
            // selected_Values[i][0]
            // make a circle
            // add it to the list
            if (i < (selected_Values.length-1)) {// only add this circle to be drawn if we haven't yet hit the max
                var polygon = new visual.Polygon({"win": psychoJS.window, "name": "polygon", "units": "height", "edges": 100, "size": selected_Values[i][0], "ori": 0.0, "pos": [selected_Values[i][1], selected_Values[i][2]], "lineWidth": 0, "colorSpace": "rgb", "lineColor": "white", "fillColor": "black", "opacity": null, "depth": 0.0, "interpolate": true});
                Circle_List.push(polygon);// add a circle to the draw list
                // increase the iteration
                i += 1;
                }
      }
         else {
            
            if (i > 0){
                
                var Cir_Pop = Circle_List.pop();//remove a circle from the draw list
                Cir_Pop.setAutoDraw(false);
                i -= 1;
                }
            }
    }
    else {
      if (keyEvents[keyEvents.length-1].pigletKey === 'space' && keyEvents[keyEvents.length-1].status === Symbol.for('KEY_UP') && ! switched) {
        if (space_func){
            space_func = false;
            }
        else{
            space_func = true;
            }
        switched = true;
    }
    }
}

What you could do is start a clock along with a brief text display before the operation starts.

Then have the operation

Then have a repeat of the text display which times out (continueRoutine = False) when the custom clock, rather than the routine timer, exceeds 6 seconds.

Hi again,

Thanks,

I do not care so much about the exact seconds (6 seconds) as I need a code by which I slow down the addition of the dots.

The way I understand what you have written, this is not an exact solution to a problem.

Not sure if this helps, but these are the closest JS sleep functions I’ve come across:

/* Two ways to make the JS script 'sleep' for delay msec.
   Both are not that good at waiting for times < 10 - 15 msec without lots of
   variability in the actual sleep time, so use with caution.
   Have not, to date, seen any performance difference between the two.
*/
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))

function sleep2(delay) {
  return new Promise((resolve) => {
    const timer = setInterval(() => {
      clearInterval(timer)
      resolve()
    }, delay);
  })
};
1 Like

Wow thanks, ill try to implement this in my code and report again