Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

542
Visualizações
js setTimeout() isn't working and is giving me a recursion error

I am trying to code chess as I play it a lot and I am trying to use setTimeout() for my move function for the pawns. I try to set it to 10 ms delay and then call it self so I am using this code:

function pawnMoveFunc(piece) {
console.log(colourToMove, mouse.x, mouse.y);
   let actualPawnX = piece.x - piece.offset;
   if (piece.colour == colourToMove) {
      if (mouse.x != actualPawnX || mouse.y != piece.y) {
         piece.x = mouse.x + piece.offset;
         piece.y = mouse.y;
         drawAfterMove();
      }

      else {
         if (mouse.x == actualPawnX && mouse.y == piece.y && stopLoop == false) {
           pawnMoveFunc(piece)
         }
      }
   }

   else {
      console.log('wrong colour');
   }
}

I am calling this function from my move function in my pawn class:

class Pawn{

   constructor(colour, x, y) {

      if (colour == 'white') {
         this.img = document.getElementById('whitePawn')
      }
      else{
         if (colour == 'black') {
            this.img = document.getElementById('blackPawn')  
         }
      }

      this.colour = colour;
      this.offset = 7.5;
      this.x = x * tileSize + this.offset;
      this.y = y * tileSize;
      this.width = 35;
      this.height = 50;
   }

   draw() {
      ctx.drawImage(this.img, this.x, this.y, this.width, this.height)
   }

   move() {
      if (this.x - this.offset == mouse.x && this.y == mouse.y) {
         setTimeout(10, pawnMoveFunc(this));
      }
      stopLoop = true;
      stopLoop = false;
   }   
}

I am calling this move function from a function that repeats calling each pawns move function all the time.

When I run this code and try to move a pawn I get a recursion error at the pawn move function but I don't know why.

Also any tips for my code would be appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are two problems in your code.

The first problem is that setTimeout takes two parameters (in order): function, and then delay. You are providing the delay parameter before the function parameter.

The second problem is that the function parameter should be a function. What you are doing is you are already calling the function instead of providing setTimeout with a function to call.

This is the updated line of code (line 28 from the top of your Pawn class):

setTimeout(() => pawnMoveFunc(this), 10);

Note: () => creates an arrow function.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda