Resetting game for Tic-Tac-Toe


When I try to reset my game either using a reset function or calling the main() function again, I get multiple copies of the all my variables, and multiple chains of events are executed at the same time.

How do I avoid this?

Do you have your code somewhere?

1 Like


This is my code. Please keep the console open!

The first game works fine, but when I reset/start the second game, my console shows two copies of the same variable, one with the initial values, and one with the last game’s final values, which messes with the game’s logic.

Thanks for helping me out!

Always be cautious with adding eventlisteners onclick or something else that can happen more than once. In this case, $('.a2b').on('click', function() { gets called more than once and that seems to mess up your code.

So try to avoid adding an eventlistener this way or use .off() which will remove all assigned eventhandlers:
$('.a2b').off().on('click', function() {
This will remove all eventlisteners from .a2b and then add the on("click".

It seemed to fix the problem.

1 Like

I found that out just five minutes ago! :smiley:

But thank you for the help!