# "continueRoutine = false" not working properly in PsychoJS

**URL:** <https://discourse.psychopy.org/t/continueroutine-false-not-working-properly-in-psychojs/40752>\
**Category:** Coding\
**Created:** [July 9, 2024, 10:01am UTC](https://discourse.psychopy.org/t/continueroutine-false-not-working-properly-in-psychojs/40752 "2024-07-09T10:01:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tan032](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@tan032](https://discourse.psychopy.org/u/tan032)\
**Post date:** [July 9, 2024, 10:01am UTC](https://discourse.psychopy.org/t/continueroutine-false-not-working-properly-in-psychojs/40752/1 "2024-07-09T10:01:42Z")

</div>

**OS** : Windows 11  
**Version** : PsychoPy 2024.1.5  
**Standard Standalone** : Yes  
**Goal** : End routine using continueRoutine = false in JS tab of code component

**Problem description** :  
I am translating a .psyexp file to JavaScript, with the help of the builder. Some custom code components break with the auto-translator. Among those is this Python code:

* * *

_Each frame:_  
if redSquare.contains(mouseRedSquare):  
continueRoutine = False

* * *

**As** you can see I check whether a mouse component “mouseRedSquare” is within the bounding box of a polygon “redSquare”, and when it is I want to end the routine.

I wrote this JS code to do that (it’s a bit of a mess):

* * *

_Each frame:_  
mouseRedSquarePos = mouseRedSquare.getPos();  
function convertToHeightUnits(mouseRedSquarePos) {  
let [mouseX, mouseY] = mouseRedSquarePos;  
let heightX = ((mouseX \* (psychoJS.window.size[0] / psychoJS.window.size[1]))/2); // Convert x to height units  
let heightY = (mouseY/2); // Convert y to height units

return [heightX, heightY];  
}

let [XRedSquare, YRedSquare] = convertToHeightUnits(mouseRedSquarePos);

ratio = psychoJS.window.size[0] / psychoJS.window.size[1]  
console.log([XRedSquare, YRedSquare]);  
console.log([-0.0625_ratio, -0.4125_ratio]);

if (-0.00625 \* ratio \<= XRedSquare && 0.00625 \* ratio \>= XRedSquare && YRedSquare \>= -0.4125 && YRedSquare \<= -0.3875) {  
continueRoutine = false;  
}

* * *

**This** code works fine the first time I encounter the routine. However, it only works once. When I later in the experiment use the same routine, placing my cursor on the red square no longer ends the routine for some reason.

**What I tried:**  
I have tried to split the final _if_ statement and the _continueRoutine = false_ using some counters and ‘==’ logic. Didn’t work.  
I tried _continueRoutine = true_ in the “begin routine” tab. Didn’t work.  
I tried to use some other logic to check whether the cursor is in the red square:

(mouseRSPos = mouseRedSquare.getPos();  
mouseRSPosHeight = [(mouseRSPos[0]/2)\*psychoJS.window.size[0]/psychoJS.window.size[1], mouseRSPos[1]/2];  
mouseRSPosPix = [mouseRSPos[0]\*psychoJS.window.size[0], mouseRSPos[1]\*psychoJS.window.size[1]];

if (redSquare.contains(mouseRSPosPix, ‘pix’)){  
continueRoutine = false;  
})

This didn’t work even once, so I disabled it.

I am at wit’s end, any help is very much appreciated!

Thanks in advance 😃

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [July 9, 2024, 10:35am UTC](https://discourse.psychopy.org/t/continueroutine-false-not-working-properly-in-psychojs/40752/2 "2024-07-09T10:35:20Z")

</div>

The contains method should auto translate. Why are you manually editing the JS code?

---

<div class="post-metadata">

**Author:** ![tan032](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@tan032](https://discourse.psychopy.org/u/tan032)\
**Post date:** [July 9, 2024, 11:22am UTC](https://discourse.psychopy.org/t/continueroutine-false-not-working-properly-in-psychojs/40752/3 "2024-07-09T11:22:06Z")

</div>

Hi wakecarter,  
Thanks for your response! That’s a fair question.

I did it manually because the auto translate does not work.

In the experiment settings, I’ve got the screen units set to height, same as the spatial units for the redSquare. I thought maybe the mouse gives its position using different units? Either way I did not manage to get the auto translate version working.

Kind regards,  
Bastiaan
