title: setTimeout not working properly inside a constructor function method
body: <p>I am trying to create an amateur mmorpg JavaScript browser game, multiplayer actually. This is a mmorpg so I am going to generate enemies randomly (doesn't matter now), the point is I have made a constructor function which creates a separate object, not linked to the prototype (which is nice). This separate object will be some individual npc, the amount will depend on how many we generate.</p>
  
  <pre><code>function myEnemy(posX,posY) {
      this.uuid = 'aa1b05',
      this.health = 2000,
      this.shield = 2000,
      this.posX = posX,
      this.posY = posY,
      this.speed = 320,
      this.damage = 80,
      this.ratio = 1000,
      //this.target = 'uuid',
      this.aggro = true,
      this.strike = function() {
          this.health -= this.damage
          console.log(this.health)
          //if(this.aggro === true) { setTimeout(this.strike(), this.ratio); }
      }
      this.attack = function() {
          if(this.aggro === true) { setTimeout(this.strike(), this.ratio); }
      }
  };
  </code></pre>
  
  <p>Don't bother about the <code>uuid</code>, it's work in progress.
  The enemy does have some basic stats, health, position, speed, etc. I have removed some methods from this example for simplicity.</p>
  
  <p>This code here spawns an enemy:
  <code>let enemy = new myEnemy(0,0)</code></p>
  
  <p>In this example I am just reducing the enemy's health each time it strikes, because I haven't implemented the targeting system yet. It's working fine, the problem comes once this.attack() is executed it's it only strikes once.</p>
  
  <p>The other problem is the reason why I commented that line in <code>this.strike()</code>, instead of repeating the action each second it repeats the action so fast that it crashes my computer.</p>
  
  <p>As far as I have gone, no matter what I try, <code>setTimeout</code> doesn't work.</p>
  
  <p>My goal is to have the enemy do the strike function while aggro is = true, I have also tried do while loops without any success, the problem is <code>setTimeout</code>.</p>
  
  <p><strong>Update</strong></p>
  
  <p>This is not a duplicate, I have also tried to remove the <code>()</code> from the strike function without success, it attacks once and then returns <code>NaN</code>, stopping the loop.</p>