JavaScript: Print 10 numbers with a delay of 3 seconds
Hari Krishna Anem
1 min read
Table of contents
// Method 1
for (let i =1; i<=5; i++) {
setTimeout(function() {
console.log(i)
}, i * 3000);
}
// Method 2
for (var i =1; i<=5; i++) {
(function foo(i) {
setTimeout(function() {
console.log(i)
}, i * 3000);
})(i) // IIFE
}
// Method 3
var i=1;
const interval = setInterval(() => {
if(i==5) {
clearInterval(interval);
}
console.log(i++);
}, 3000);
0
Subscribe to my newsletter
Read articles from Hari Krishna Anem directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Hari Krishna Anem
Hari Krishna Anem
Full stack developer (ReactJS, NodeJS, JavaScript, PHP, SQL)