Resetting game for Tic-Tac-Toe

Resetting game for Tic-Tac-Toe
0.0 0

#1

Hi!

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?


#2

Do you have your code somewhere?


#3

Hi!

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!


#4

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.


#5

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

But thank you for the help!