![]() |
How not to add multiple listeners
Hello,
The code below is called when a user clicks a 'Submit' button. The problem is the line highlited below, each time they click it adds an additional listener so the event fires twicethe second time, three times the third time, etc. What is the best way to avoid adding unwanted listeneres like this? Code:
function checkAnswer() |
At the start of the code, set a global variable to false. Then, when the link is clicked, set it to true. Put a conditional around the highlighted line - if variable = false { code }. That way, if the variable is true, it won't fire.
|
I would just define the click handler outside the checkAnswer() function.
|
Another alternative would be to have the event listener remove itself again when it runs - that way it can only run once for each time it is added.
|
| All times are GMT +1. The time now is 01:02 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.