Passing an equation as a parameter in javascript
<pre><code>newnooftimes = nooftimes + 1;
if (nooftimes &lt; 10) {
    setTimeout("roll(newnooftimes);", 150);
}
</code></pre>

<p>vs</p>

<pre><code>if (nooftimes &lt; 10) {
    setTimeout("roll(nooftimes + 1);", 150);
}
</code></pre>

<p>Why doesn't the latter work, even though the former works?</p>

<p>EDIT: This is my entire function. Seems like there might be some issue with variables running out of scope? I'm new to javascript so would appreciate some help to point me to what went wrong. This function is using the suggested code in the reply before, but it still doesn't work. roll() is only called once, by another call outside the function. The recursion never happens - why?</p>

<pre><code>function roll(nooftimes) {
    ctx.clearRect(dicex,dicey,diceWidth,diceHeight); //clears the space where the dice face may have been already drawn
    var roll = 1+Math.floor(Math.random()*6);
    drawFace(roll); 
    if (nooftimes &lt; 10) {
        setTimeout(function () { roll(nooftimes + 1); }, 150);
    }
}
</code></pre>
