I'm experimenting with creating a loop that repeats a specific action using setTimeout in JavaScript.
setTimeout(function() {
console.log("Hey!");
setTimeout(function() {
console.log("Hey!");
setTimeout(function() {
console.log("Hey!");
}, 1000);
}, 1000);
}, 1000);
However, my attempt at it looks like this:
for (i = 0; i < 3; i++){
setTimeout(function() {
console.log("Hey!");
}, 1000);
}
Unfortunately, the code is not functioning as expected.
Upon further investigation, I've discovered that the issue lies in the fact that the setTimeout calls are overlapping due to each iteration of the loop. Is there a way to resolve this?