I want to print the array in descending order with a setTimeout. I don't know why the below code is not working in descending order.
let delay = 1000;
let array = [1, 2, 3, 4, 5];
for(let i = array.length; i > 0; i--) {
setTimeout(() => {
console.log(array[i - 1]);
}, delay * i);
}
Expected output is 5 4 3 2 1
with one second delay. But it's printing 1 2 3 4 5
If I remove the i
from setTimeOut()
it will output as expected but won't work with the delay.
Really appreciate the help. Thanks.