title: Interval within function gets called hundreds of times
body: <p>When I call the function once, the interval is ok, it is added and cleared as supposed to. When the same function gets called again, the interval is called hundreds of times.
  How can I stop this behavior and what is wrong with my function?</p>
  
  <pre><code>function draw(){
      ctx.clearRect(0,0,width,height);
      ctx.fillStyle = 'blue';
  
      // Add cube width 5px
      if(cube.width &lt;= 300){
          cube.width += 5;
      }
  
      // Draw it on the screen
      ctx.fillRect(cube.x,cube.y,cube.width,cube.height);
  
      // Create function b(e) to check cubes collision
      function b(e){
          var x = e.clientX - ctx.canvas.offsetLeft;
          var y = e.clientY - ctx.canvas.offsetTop;
  
          if(x &gt;= cube.x &amp;&amp; x &lt;= cube.x + cube.width
              &amp;&amp; y &gt;= cube.y &amp;&amp; y &lt;= cube.y + cube.height){
              ctx.canvas.removeEventListener('click',b,false);
              level1();
              return;
          }
      }
  
      ctx.canvas.addEventListener('click', b, false);
  
      // Set the interval to 60 FPS
      var timer = setInterval(function(){
          check += 1;
  
          console.log(check);
  
          if(check &gt; 50){
              return;
          }
          // Call again the same function
          draw();
      },1000/60);
  }
  </code></pre>
  
  <p>I suspect this is a javascript problem, so my guess is that canvas has nothing to do with this.</p>
  
  <p><strong>Update</strong>
  The function I am calling draw from (which is inside another function):</p>
  
  <pre><code>    function a(e){
          var x = e.clientX - ctx.canvas.offsetLeft;
          var y = e.clientY - ctx.canvas.offsetTop;
  
          if(x &gt;= player.x &amp;&amp; x &lt;= player.x + player.width
              &amp;&amp; y &gt;= player.y &amp;&amp; y &lt;= player.y + player.height){
              ctx.canvas.removeEventListener('click',a,false);
              draw();
          }
      }
  
      // Add the click event using the previous function
      ctx.canvas.addEventListener('click', a, false);
  </code></pre>