+ 1

How do i stop this function? [SOLVED]

function moveMyPlayer() { window.addEventListener('mousemove', function (e) { $("#player").css("transform","translate3d( " + e.x + "px, " + e.y + "px, 0px)") }); }

14th Aug 2020, 10:30 AM
Pieter Edwards
Pieter Edwards - avatar
2 Antworten
+ 1
The problem is because i am using a variable for this line $("#player").css("transform","translate3d( " + e.x + "px, " + e.y + "px, 0px)") it is continuously updating the css so the 'player just follows the mouse around the screen'
14th Aug 2020, 11:31 AM
Pieter Edwards
Pieter Edwards - avatar
+ 4
You could you removeEventListener() to stop listening to an event. One change you have to make is to define the function separately rather than using it as anonymous function. Example: function myFunc() { //Some code } function moveMyPlayer() { window.addEventListener('mousemove', myFunc); } function stopMyPlayer() { window.removeEventListener('mousemove', myFunc); }
14th Aug 2020, 10:48 AM
Raj Chhatrala
Raj Chhatrala - avatar