Function Definitions as Arguments
<pre><code>var cancel = setTimeout(function(){clearTimeout(cancel);}, 500);

var cancel = setTimeout(clearTimeout(cancel), 500);
</code></pre>

<p>Scholastic question: The first of these two expressions work, while the second does not.  The setTimeout() method is accepting a function and a duration as its arguments and both of these examples are clearly providing that.  The only difference is that the first is a function definition while the second is a function invocation.</p>

<p>If functions designed to take a function as an argument can only handle function definitions, <em>how do you go about providing that function with the variables it may need?</em> For example:</p>

<pre><code>stop = function(y){clearInterval(y)};
count = function(x){
    var t = 0,
    cancel = setInterval(function(){console.log(++t);},1000);
    setTimeout(stop(cancel),x);
};
count(5000);
</code></pre>

<p>The function above doesn't work because it's invoking the function</p>

<pre><code>stop = function(){clearInterval(cancel)};
count = function(x){
    var t = 0,
    cancel = setInterval(function(){console.log(++t);},1000);
    setTimeout(stop,x);
};
count(5000);
</code></pre>

<p>The function above doesn't work because the stop() doesn't have access to the cancel variable.</p>

<p>Thank you in advance for attempting to educate me on the work-around for this type of issue.</p>
