Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post

    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()
    {
    	alert(chosenAnswer);
    	if(chosenAnswer == correctAnswer)
    	{		
    		var xmlFeedBack = "";
    		$(questionArr[questionIndex]).find('answer').each(function(){
    			if($(this).find('aText').text() == chosenAnswer)
    			{
    				xmlFeedBack = $(this).find('feedback').text();
    				alert(xmlFeedBack);
    			}
    		});
    
    		$("#feedback").html('<img src="../js_know_check/course_assets/kcbtnsprite.png" /><br/>' + xmlFeedBack + "<br/><br/>Click This Message Box to Continue.");
    		questionIndex++;
    		$("#feedback").click(function (evt) {alert("You clicked the message box! Great");displayQuestion()});
    		numCorrect++;
    	}
    }

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,217
    Thanks
    12
    Thanked 338 Times in 334 Posts
    I would just define the click handler outside the checkAnswer() function.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,593
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •