newnooftimes = nooftimes + 1;
if (nooftimes < 10) {
setTimeout("roll(newnooftimes);", 150);
}
対
if (nooftimes < 10) {
setTimeout("roll(nooftimes + 1);", 150);
}
前者が機能するのに、後者が機能しないのはなぜですか?
編集:これは私の機能全体です。範囲外の変数に問題があるように見えますか? 私はjavascriptが初めてなので、何がうまくいかなかったのかを教えていただければ幸いです。この関数は以前の返信で提案されたコードを使用していますが、まだ機能しません。roll() は、関数外の別の呼び出しによって一度だけ呼び出されます。再帰は決して起こりません - なぜですか?
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 < 10) {
setTimeout(function () { roll(nooftimes + 1); }, 150);
}
}