0

I have added an event Listener on click event for Html canvas element. The function statements gets executed multiple time. first time the statements execute once, when i call the main fnction again, the click causes same function to execute twice and then thrice and so on

Here is the listener

 function initializeNewGame() {

        // Set block 
        cur = new Date();
        countDownDate = new Date(cur.getTime() + 5*60010).getTime();
        moves = 0;
        BLOCK_WIDTH = Math.round(BLOCK_IMG_WIDTH / TOTAL_COLUMNS);
        BLOCK_HEIGHT = Math.round(BLOCK_IMG_HEIGHT / TOTAL_ROWS);

        document.getElementById("canJigsaw").addEventListener("click", myFunction);

        function myFunction() {
            moves = moves + 1;  

            } 

        SetImageBlock();

        DrawGame();
    }
2
  • 2
    Are you calling this block of code several times - it adds a new event handler every time. Make sure you only call the addEventListener bit once and once only Commented Aug 9, 2017 at 9:31
  • @Jamiec : it worked . i added a boolean to make sure the function if not called more than once. Thank you Commented Aug 10, 2017 at 5:46

1 Answer 1

1

Make sure that initializeNewGame is only called once. Each time it is called, it will add another click handler. This means that myFunction could be called multiple times for each click.\

Try adding a console log in the initializeNewGame to help detect if its called more than once.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.