Share Posted March 17 Hello! I have a timeline. I want tl.play() to work on a computer and not work on a phone. The click listener is added like this: element.addEventListener("click", handler) The click listener is removed like this: element.removeEventListener("click", handler) if you add a listener like this: element.addEventListener("click", ()=>handler()) then this listener can't be removed (I couldn't) My problem is that tl.play is only added like this: element.addEventListener("click", ()=>tl.play()) This means I can't remove it. Help me please. See the Pen GRXGbwM?editors=1011 by romanusname (@romanusname) on CodePen Link to comment Share on other sites More sharing options...
Solution Solution Share Posted March 17 I think that will only work if you assign the arrow function to a variable as removeEventListener requires a reference to the function. See the Pen 58144ad0414b9266b9556f00d0b0f91c by PointC (@PointC) on CodePen 1 1 Link to comment Share on other sites More sharing options...
Share Posted March 17 @PointC Indeed. @Romanus You have to first define the handle function and then use that handler function inside the event listener. Later, you will be able to easily remove that event listener when needed. Check the example. There you can see that for first 2 second you are able to click the square (still have the event listener). But, after 2 seconds event listener is removed. See the Pen QWVxeYw by demiavaliani (@demiavaliani) on CodePen 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now