Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!