Reputation:
I want my loop to count down every 200ms.
let myArr = [3, 4, 5, 6];
for (let i = myArr.length - 1; i >= 0; i--) {
console.log(myArr[i]); // execute this console.log every 200ms. (not 200 MS after the last one.)
}
Upvotes: 0
Views: 54
Reputation:
It makes much more sense to use setInterval
for this than any for-loop, as setInterval
was designed to execute code every x
milliseconds:
let myArr = [3, 4, 5, 6];
var i = myArr.length - 1;
let interval = window.setInterval(() => {
if (i >= 0) {
console.log(myArr[i]);
i--;
} else {
window.clearInterval(interval);
}
}, 200);
Upvotes: 1