2

Hey I try to bulid a minesweeper game so I create the game and I call emptyCell When user click on empty cell (cell box in the game) now I get this error:

Uncaught RangeError: Maximum call stack size exceeded

So I search and I found this answer Chrome/jQuery Uncaught RangeError: Maximum call stack size exceeded that says:

You can also get this error when you have an infinite loop. Make sure that you don't have any unending, recursive self references.

So I check and realy the functions call to her self over 500 times I dont know why, I have only 100 cells on my page this is the function:

function emptyCell(cell, x, y) {
  cell.className = "show";
  var i, Next, Prev;
  Next = y + 1;
  Prev = Prev + 1;
  for (i = x - 1; i <= x + 1; i++) {
    if (Game[Next] !== undefined && Game[Next][i]) {
      if (Game[Next][i] == 0) emptyCell(GameBoxes[Next][i], i, Next);
      else {
        GameBoxes[Next][i].innerHTML = Game[Next][i];
        GameBoxes[Next][i].className = "show c" + Game[Next][i];
      }
    }
    if (Game[Prev] !== undefined && Game[Next][i]) {
      if (Game[Prev][i] == 0) emptyCell(GameBoxes[Prev][i], i, Prev);
      else {
        GameBoxes[Prev][i].innerHTML = Game[Prev][i];
        GameBoxes[Prev][i].className = "show c" + Game[Prev][i];
      }
    }
  }
  if (Game[y] !== undefined && Game[y][x - 1] !== undefined) {
    if (Game[y][x - 1] == 0) emptyCell(GameBoxes[y][x - 1], x - 1, y);
    else {
      GameBoxes[y][x - 1].innerHTML = Game[y][x - 1];
      GameBoxes[y][x - 1].className = "show c" + Game[y][x - 1];
    }
  }
  if (Game[y] !== undefined && Game[y][x + 1] !== undefined) {
    if (Game[y][x + 1] == 0) emptyCell(GameBoxes[y][x + 1], x + 1, y);
    else {
      GameBoxes[y][x + 1].innerHTML = Game[y][x + 1];
      GameBoxes[y][x + 1].className = "show c" + Game[y][x + 1];
    }
  }
}
Community
  • 1
  • 1
Harman met
  • 241
  • 2
  • 3
  • 10
  • 2
    Set a `debugger;` statement in the function then step through it with the Developer Tools. You've got calls to `emptyCell` inside of `emptyCell` which are probably passing in the same arguments (i.e., you're calling `emptyCell(cell, 0, 0)` over and over again). This is a lot of code to sift through. – Mike Cluck Mar 08 '16 at 18:38
  • IOW, an infinite recursion loop. – gitsitgo Mar 08 '16 at 18:41
  • This is not very maintainable code; maybe there are more coding errors. Please refactor you code first and make some unit-tests. – Jeroen Heier Mar 08 '16 at 18:45
  • @MikeC I improved the code, maybe take a look again? – Harman met Mar 08 '16 at 19:04
  • @Harmanmet Like I said, step through your code. I can't "see" the problem. I'd have to run it and step through it myself. – Mike Cluck Mar 08 '16 at 19:07

0 Answers0