With this code i want to create a loop that will work in decreasing way. I want to use for loop going from 10 till 1. I did this, but i have an error. The error is next: after pushing on the button the last values have a delay.
Probably this is happening because the last indexes are higher at the end, and when the app divide 1000 / 2, the timeout is higher. Am i right? and how to solve the issue?
const btn = document.getElementById('btn');
btn.addEventListener('click', function(){
for (let index = 10; index > 0; index--) {
setTimeout(() => {
console.log(index)
}, 1000 / index);
}
})
<div class="app">
<button id="btn">click</button>
</div>
1000 / indexis, for index in [1, 10]. Long story short, y = 1/x is not linear.for (let i = 10; --i;) { }. Is the delay something you want? What is the result you want? Please be more clear.