+ 3

removeEventHandler not working!!!!!

Basically, trying to remove the click event from this program and it just doesnt work. Tried to look up online articles about it but none talk about removing multiple click events in a for loop. Can someone please help? How the program works: You hit twice on the board then the program removes the click event. I've tried to put this in but the function just seens to not want to run properly so the click event never gets removed.... https://code.sololearn.com/W7VDb2fWdNvp/?ref=app

16th Jan 2019, 8:03 PM
Jay
Jay - avatar
15 Respostas
+ 3
Heres my attempt Jay . Look in the test function and I changed the removeEvents call to the remove event listener and I removed the bind call that Aaron made. It's not yet working properly, but I think you can solve it. It's at least removing the listeners now https://code.sololearn.com/WEZLfzvWQrz8/?ref=app
16th Jan 2019, 8:55 PM
Zeke Williams
Zeke Williams - avatar
+ 3
Oh yeah, it can definitely get frustrating. I always have to think on problems like these: "someone MUST have had this idea at least once before." It motivates me to make the right Google search
16th Jan 2019, 9:35 PM
Zeke Williams
Zeke Williams - avatar
+ 2
I think if you remove a event listener JS wants you to use exactly the same function as the one you used when adding the event listener. Therefore you can't declare a function inside addEventListener() if you want to remove the event later. Instead you need to declare the function first and then use it in addEventListener() later so JS can make sure it's actually the same function when you call removerEventListener(). I hope that made sense. I'm not completely sure if that's correct but I had a similar problem few days ago and I think that's been the solution. Here's how it would look like in your code: https://code.sololearn.com/WkRERnSsncOe/?ref=app
16th Jan 2019, 8:29 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
Well, now my code works in terms of event listeners. However you need to find a workaround for passing arguments to the event listener, maybe by using event.target or event.currentTarget. It turns out the binding arguments by using func.bind() in addEventListener as I tried doesn't work.
16th Jan 2019, 8:50 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
Yep Aaron Eberhardt using the event target is what I did. I think thats the way you should go
16th Jan 2019, 8:56 PM
Zeke Williams
Zeke Williams - avatar
+ 2
Thing is is that I read MDN, SL and Stack but sometimes its just so frustrating đŸ˜„đŸ˜„đŸ˜„
16th Jan 2019, 9:25 PM
Jay
Jay - avatar
+ 1
Maybe you have to use the named function, instead of putting the named function inside of a lambda function? That's my best guess at the moment. I'll keep looking at it though
16th Jan 2019, 8:28 PM
Zeke Williams
Zeke Williams - avatar
+ 1
Thanks just tried it. Its still going after i clicked on it twice. So its not removing the event
16th Jan 2019, 8:33 PM
Jay
Jay - avatar
+ 1
I call any unnamed function a lambda, which might be incorrect Jay , but Aaron Eberhardt is actually talking about the same idea.
16th Jan 2019, 8:34 PM
Zeke Williams
Zeke Williams - avatar
+ 1
Thanks very much Aaron Eberhardt and Zeke Williams. Is there a way to learn Javascript efficiently? I feel like I know a lot... and then a I get hit with a new problem 😂
16th Jan 2019, 8:59 PM
Jay
Jay - avatar
+ 1
No problem! Honestly, there comes a point in every language I'm learning where I can't learn anything else until I start reading the actual language documentation 😂
16th Jan 2019, 9:01 PM
Zeke Williams
Zeke Williams - avatar
+ 1
Jay I guess I've learned it on SL and the rest from MDN and stackoverflow
16th Jan 2019, 9:02 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
0
Aaron Eberhardt it said that i is not defined
16th Jan 2019, 8:30 PM
Jay
Jay - avatar
0
Zeke Williams lambdas are used in js? Im not sure what you mean
16th Jan 2019, 8:31 PM
Jay
Jay - avatar