+ 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)") }); }
2 ответов
+ 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'
+ 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);
}