0

I've noticed when using setInterval() with objects, it accepts this:

setInterval(function(){auto1.increaseValue(s);}, auto1.interval);

Where auto1 is an object from a class. However, the program throws an error when I put an object or object of an array of another object inside the function. Ex.

    let index = this.arrayForItems.length-1;
    setInterval(function(){this.arrayForItems[index].increaseValue(s);header.innerHTML = s.value;}, this.arrayForItems[index].interval);
    setInterval(function(){this.item.increaseValue(s);header.innerHTML = s.value;}, this.item.interval);//Where item is the object inside object shop

But this works.

    let index = this.arrayForItems.length-1;
    let referenceToPurchase = this.arrayForItems[index];
    setInterval(function(){referenceToPurchase.increaseValue(s);header.innerHTML = s.value;}, referenceToPurchase.interval);

Why is this so?

Justin Wu
  • 1
  • 1
  • 2
    What is the error? Could it be related to behavior of the [`this` keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)? – showdev Apr 16 '22 at 19:11
  • Add a console.log(this) statement to each and compare the result. – Yogi Apr 16 '22 at 19:28

0 Answers0