Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda