title: setInterval using a non anonymous function requiring parameters has to be inside an anonymous function. Why?
body: <p>Ok I have reviewed several postings here and elsewhere regarding setInterval in jquery/javascript the annoying thing about the answers is that I am not learning why the solutions work.</p>
  
  <p>Please consider:</p>
  
  <p>Using an anonymous function we can set an alert to repeatedly output "bunnies":</p>
  
  <pre><code>setInterval(function(){
    alert("bunnies")
  },3000);
  </code></pre>
  
  <p>But if we want to use a non anonymous function we have to code</p>
  
  <blockquote>
    <p>setInterval(hop,3000);</p>
  </blockquote>
  
  <p>where funct:</p>
  
  <pre><code>function hop(){
      alert("bunnies");
  }
  </code></pre>
  
  <p>If we attempt to code:</p>
  
  <pre><code>setInterval(hop(),3000);
  </code></pre>
  
  <p>hop is executed but once only. I do not understand why this is. I have read various SO's on this which imply that we need to be passing a reference to setInterval. Does this imply that the first form setInterval(hop,3000); passes by reference. If so could this be explained?</p>
  
  <p>Therefore we have an issue. In that obviously it would be desireable to be able to pass a parameter to function hop like .....</p>
  
  <pre><code>setInterval(hop("bunnies"),3000);
  </code></pre>
  
  <p>where funct:</p>
  
  <pre><code>function hop(msg){
      alert(msg);
  }
  </code></pre>
  
  <p>This does cause hop to be invoked and "bunnies" to be output but again the function is invoked once only.</p>
  
  <p>So as far as I can work out the only way to pass a parameter to a function being controlled by setInterval is to incorporate it inside an anonymous function:</p>
  
  <pre><code>setInterval(function(){
   hop("bunnies")
  },3000);
  </code></pre>
  
  <p>this passes the parameter and repeats the execution of hop alerting us to bunnies every 3 seconds (very important to be alert to bunnies).</p>
  
  <p>Questions therefore:</p>
  
  <ol>
  <li>Is this the only syntax that will allow you to pass a parameter in.</li>
  <li>Why does setInterval(hop("bunnies"),3000); not work.</li>
  </ol>