javascript(set Timeoutでset Intervalではなく)

576 ワード

 
  
var num = 0;
var max = 10;
function incrementNumber() {
num++;
//if the max has not been reached, set another timeout
if (num < max) {
setTimeout(incrementNumber, 500);
} else {
alert(“Done”);
}
}
setTimeout(incrementNumber, 500);
setIntervalを使うと次回の呼び出しがあるかもしれません.前回の呼び出し前に呼び出します.
True interval s are rarely used in production environments because it's possible that one interval begin before the previous one has finished executing)