I've very new to javascript, and im having some weird issues. Im trying to fade out an image, note that the code for this is not yet complete. To my knowledge, this should work. but doesnt. Here's the code.
function changeimg() {
var allElements = document.getElementsByClassName('picture');
console.log(allElements.length);
for (var i = 0; i<allElements.length;i++) {
var opac = window.getComputedStyle(allElements[i]).opacity;
if (opac > 0.2) {
fadeout(allElements[i]);
break;
}
}
}
function fadeout(element) {
console.log("reduce opacity");
var opac2 = window.getComputedStyle(element).opacity;
element.style.opacity = opac2 - 0.05;
if(opac2 > 0.2) {
setTimeout(fadeout(element), 100);
}
}
This is meant to find the image that is now visible, and fade it out. this works, but it happens instantly. No matter what value i give to the setTimout() method, it never uses it, and completes it almost instanly.
I've looked around on the internet, and it seems i need to lose the () on my fadeout() method. But how then can i give the fadeout method the element which to fade out?
thanks in advance