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);
If you use setInterval, the next call may occur Called before the previous call (
True intervals are rarely used in production environments because it ' s possible that one interval will begin before the previous one has finished executing)