title: Create bullets for a game at an interval
body: <p>I have a game, and in it there is a main, controllable character and then enemies that shoot back at the character. For the enemies, when they shoot back I want them to shoot at intervals so that it isn't just one massive block of bullets, and it worked with a setInterval for one, but when a second enemy comes in they don't shoot. Only one of the two will. If anybody has a solution that would be great!</p>
  
  <pre><code>            function enemies() {
          if (enemy_soldiers.length == 0) {
              level += 0.2;
              for (var i = 0; i&lt;(1 + Math.floor(Math.round(level))); i++) {
                  var gx = 1450
                  var gy = getRandom(430, 630);
                  enemy_soldiers.push({
                      x: gx,
                      y: gy,
                      l: gl,
                      d: getRandom(350, 600),
                      shooting: false,
                      interval: setInterval (function() {enemy.shooting = true;},fire_rate),
                      shoot: function() {
                          enemy_bullets.push({
                              x: enemy.x+40,
                              y: enemy.y+87,
                              vel: 10,
                      });
                      }
                  });
              }
          }
          var enemy;
          gctx.clearRect(0, 0, 1400, 800);
          for (var i in enemy_soldiers) {
              enemy = enemy_soldiers[i];
              drawenemy(enemy.x, enemy.y, enemy.l);
              //ai
              if (distance(enemy.x, enemy.y, cx, cy) &gt;= enemy.d &amp;&amp; enemy.x&gt;cx) {
                  enemy.x-=vel;
              }
              else if (distance(enemy.x, enemy.y, cx, cy) &gt;= enemy.d &amp;&amp; enemy.x&lt;cx) {
                  enemy.x+=vel;
              }
              if (distance(enemy.x, enemy.y, cx, cy) &lt;= 600) {
                  if (enemy.shooting == true) {
                      enemy.shoot(enemy.x,enemy.y);
                      enemy.shooting = false;
                  }
                  gbctx.clearRect(0, 0, 1400, 800);
                  for (var j in enemy_bullets) {
                      enemy_bullet = enemy_bullets[j];
                      enemy_bullet.x -= enemy_bullet.vel;
                  if (enemy_bullet.x &gt; 1400 || enemy_bullet.x &lt; -5 || enemy_bullet.y &gt; 800 || enemy_bullet.y &lt; -5) {
                      enemy_bullets.splice(j,1);
                  }
                  drawEnemyBullet(enemy_bullet.x, enemy_bullet.y);
              }
          }
      }}
  </code></pre>