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

152
Visualizações
Can't update object property when running setInterval

I was trying to change the property for the objects inside this._keys. The method I am using worked fine to change the property but the problem is I have an if statement that decides if the property gets changed or not and that if statement checks if a variable is equal to a string. That variable is dynamic so I have to setInterval to keep on updating the data. But when I setInterval I cant update the property of the object anymore. I get the error Cannot set properties of undefined (setting 'forward'). Why can't I change the property while updating the data? I have commented on my code to better point out the problem. Any help on why this occurs and how to fix it is appreciated. Thanks in advance.

const currentKeylogW, currentKeylogA,
  currentKeylogD, currentKeylogS = a dynamic data;


class BasicCharacterControllerInput2 {
  constructor() {
    this._Init();
  }
  
  
  _Init() {

    this._keys = {
      forward: false,
      backward: false,
      left: false,
      right: false,
      space: false,
      shift: false,
      shiftForward: false,
    };

    //this has no error but currentKeylog's variables are not being updtes so its useless
    if (currentKeylogW == '"w"') {
      this._keys.forward = true;
    } else if (currentKeylogA == '"a"') {
      this._keys.left = true;
    } else if (currentKeylogD == '"d"') {
      this._keys.left = true;
    } else if (currentKeylogS == '"s"') {
      this._keys.right = true;
    } else if (currentKeylogW == '"!w"') {
      this._keys.forward = false;
    } else if (currentKeylogA == '"!a"') {
      this._keys.left = false;
    } else if (currentKeylogD == '"!d"') {
      this._keys.left = false;
    } else if (currentKeylogS == '"!s"') {
      this._keys.right = false;
    }


    //updates currentKeylog's variables but can't set forward,.. properties
    setInterval(function() {

      if (currentKeylogW == '"w"') {
        this._keys.forward = true;
      } else if (currentKeylogA == '"a"') {
        this._keys.left = true;
      } else if (currentKeylogD == '"d"') {
        this._keys.left = true;
      } else if (currentKeylogS == '"s"') {
        this._keys.right = true;
      } else if (currentKeylogW == '"!w"') {
        this._keys.forward = false;
      } else if (currentKeylogA == '"!a"') {
        this._keys.left = false;
      } else if (currentKeylogD == '"!d"') {
        this._keys.left = false;
      } else if (currentKeylogS == '"!s"') {
        this._keys.right = false;
      }

    }, 500)

  }
}

about 4 years ago · Juan Pablo Isaza
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