0

I would like to call a function in a variable which has other functions nested in it and store a return value in the variable.

However, the return value is in the nested function. I have already tried something, but this does not work so well and unfortunately I am also not the top Javascript developer and therefore unfortunately do not know the name of the functions/methods I am looking for.

So I have two questions: 1. what is the "best practice" variant for my plan? 2. what would my little example look like: https://jsbin.com/jupijaquce/1/edit?html,output

Here again as code:

<div id="gestureZone" style="width: 100%; height: 80%; background: red;"></div>

<script>
    var mousePosition = mouseDistance('gestureZone');

    function mouseDistance($id) {
        document.getElementById($id).onmousemove = handleMouseMove;

        function handleMouseMove() {
            console.log(event.clientX, mousePosition);
            document.getElementById($id).innerText = event.clientX;

            // This should update on change the mousePosition var
            return event.clientX;
        }

    }
</script>
Dieter Information
  • 1,045
  • 2
  • 8
  • 13

0 Answers0