title: How to pass intervalID to interval function in javascript?
body: <p>In javascript when I create an interval, I want to stop the interval from inside the function, but I don't want to reference the ID value from outside like this</p>
  
  <pre><code>var y = setInterval(function(){ clearInterval(y); }, 1000);
  </code></pre>
  
  <p>What I want is to pass a variable similar to this style</p>
  
  <pre><code>setTimeout(function(data){alert(data);}, 1000, "data");
  </code></pre>
  
  <p>This works for setInterval too, except I can't really pass the id value that's returned by the setInterval function, because it gets created after calling it.</p>
  
  <p>Right now I'm doing a hack like this:</p>
  
  <pre><code>var r = [];
  var y = setInterval(function(r){ if (r.length==1) { clearInterval(r[0]); } }, 1000, r);
  r.push(y);
  </code></pre>
  
  <p>Does anyone know the right way?</p>
  
  <p>Thanks</p>