I'm aware of closures, IIFE.
I've read the other answers (they all point to using IIFE).
So why is this not working?
- my image should gradually fade-in (in 2s)
- it seems like it's only rendering once (with final value)
var imgFade = document.getElementById('img-fade');
for(i = 0; i < 100; i++){
(function(step) {
setTimeout(function() {
imgFade.style.opacity = (step/100);
}, 20);
})(i);
}
here's the code: https://jsfiddle.net/warkentien2/Lh10phuv/1/
EDIT: for future readers
consider all answers transitioning from i = 1; to 1 <= 100, i++
so it won't stop rendering at 99%