setInterval
1 var num = 0; 2 var max = 10; 3 var intervalId = null; 4 5 function incrementNumber() { 6 num++; 7 8 if (num == max) { 9 clearInterval(intervalId); 10 console.log("Done"); 11 } 12 } 13 14 intervalId = setInterval(incrementNumber, 500);
setTimeout
1 var num = 0; 2 var max = 10; 3 4 function incrementNumber() { 5 num++; 6 console.log(num); 7 8 if (num < max) { 9 setTimeout(incrementNumber, 500); 10 } else { 11 console.log("Done"); 12 } 13 } 14 15 setTimeout(incrementNumber, 500);