I have a function that contains other functions and event listeners.
How can I end/exit them all when a condition is met?

The function is called when a button is clicked and adds points on click-events and then visualizes a line.
Now I want to stop the function "drawLine" when a maximal amount of points is reached or when the line is closed (selected point == first point).

A return statment at the beginning did not fix that for me:

The function:

```
drawLine() {
            if(buildingPts.length >= 5)
            {
                console.log("Max points reached")
                return
            }
            var line; 

            //Raycaster for drawing tool
            const raycaster = new THREE.Raycaster()
            var plane = new THREE.Plane( new THREE.Vector3( 0, 0, 1), 0 );
            const clickMouse = new THREE.Vector2()
            //Geometry
            var geometry = new THREE.BufferGeometry();
            var MAX_POINTS = 500;
            var positions = new Float32Array(MAX_POINTS * 3);
            geometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));
            //Material
            var material = new THREE.LineBasicMaterial({ color: 0xff0000, linewidth: 2 });
            //Line
            line = new THREE.Line(geometry, material);
            scene.add(line);

            window.addEventListener("mousemove", onMouseMove, true);
            window.addEventListener('mousedown', onMouseDown, true);

            function updateLine() {
                positions[count * 3 - 3] = pos.x;
                positions[count * 3 - 2] = pos.y;
                positions[count * 3 - 1] = pos.z;
                line.geometry.attributes.position.needsUpdate = true;
            }

            //Mouse move handler
            function onMouseMove(event) {
                mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
                mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
                mouse.z = 0;
                raycaster.setFromCamera( mouse, camera );
                var intersects = new THREE.Vector3();
                let planeIntersect = raycaster.ray.intersectPlane( plane, intersects );
                pos = new THREE.Vector3( Math.round(planeIntersect.x), Math.round(planeIntersect.y), 0);
                mouse.unproject(camera);
                if( count !== 0 ){
                    updateLine();
                }
            }

            //Add point
            function addPoint(event){
                console.log("point nr " + count + ": " + pos.x + " " + pos.y + " " + pos.z);
                positions[count * 3 + 0] = pos.x;
                positions[count * 3 + 1] = pos.y;
                positions[count * 3 + 2] = pos.z;
                buildingPts.push(pos)
                count++;
                line.geometry.setDrawRange(0, count);
                updateLine();
            }

            //Mouse down handler
            function onMouseDown(evt) {
                // on first click add an extra point
                if( count === 0 ){
                    addPoint();
                }
                addPoint();
            }
        },
animate() {
        renderer.setAnimationLoop(() => {
            composer.render();
            renderer.render(scene, camera);
        });
    },

```