How to Implement a Loop in JavaScript Code

I’m trying to develop a loop for the following JavaScript code. Could someone assist me with this?

function fadeTo90() { document.getElementById("myElement").style.opacity = "0.90"; setTimeout(fadeTo80, 100); }
function fadeTo80() { document.getElementById("myElement").style.opacity = "0.80"; setTimeout(fadeTo70, 100); }
function fadeTo70() { document.getElementById("myElement").style.opacity = "0.70"; setTimeout(fadeTo60, 100); }
function fadeTo60() { document.getElementById("myElement").style.opacity = "0.60"; setTimeout(fadeTo50, 100); }
function fadeTo50() { document.getElementById("myElement").style.opacity = "0.50"; setTimeout(fadeTo40, 100); }
function fadeTo40() { document.getElementById("myElement").style.opacity = "0.40"; setTimeout(fadeTo30, 100); }
function fadeTo30() { document.getElementById("myElement").style.opacity = "0.30"; setTimeout(fadeTo20, 100); }
function fadeTo20() { document.getElementById("myElement").style.opacity = "0.20"; setTimeout(fadeTo10, 100); }
function fadeTo10() { document.getElementById("myElement").style.opacity = "0.10"; setTimeout(hideElement, 100); }

I composed the following code. Is it appropriate? If not, could you provide corrections?

function clearMessage() {
    for (let index = 1; index < 10; index++) {
        setTimeout(() => { document.getElementById("myElement").style.opacity = 100 - (index * 10); }, 100);
    }
}

Thank you!

To achieve the desired fading effect using a loop, you'll need to make a few adjustments to your code. The current loop sets all timeouts at the same time, which doesn't result in the progressive fading effect you're looking for. Instead, you should multiply the delay in the setTimeout function by the loop index to ensure each step has a delay that builds upon the previous one.

Here's a corrected version of your code:

function clearMessage() {
    for (let index = 1; index <= 9; index++) {
        setTimeout(() => {
            document.getElementById("myElement").style.opacity = (100 - (index * 10)) / 100;
        }, index * 100);
    }
    setTimeout(hideElement, 1000);
}

function hideElement() {
    document.getElementById("myElement").style.opacity = "0";
}

Explanation:

  • The loop iterates from 1 to 9, representing the sequence of fading steps from 90% to 10% opacity.
  • Each setTimeout is triggered in increments of 100 milliseconds multiplied by the index of the loop to create a staggered effect. This ensures that each step occurs in sequence.
  • The opacity value calculation is adjusted by dividing by 100 to convert it to a decimal, as CSS opacity values range from 0.0 to 1.0.
  • After the loop, the hideElement function sets the opacity to 0, effectively hiding the element, and is triggered at the end of the sequence with an appropriate delay.

clearMessage() function works, but tweak setTimeout like this to delay each step:

function clearMessage() {
    for (let index = 1; index <= 9; index++) {
        setTimeout(() => {
            document.getElementById("myElement").style.opacity = (100 - (index * 10)) / 100;
        }, index * 100);
    }
    setTimeout(hideElement, 1000);
}

function hideElement() {
    document.getElementById("myElement").style.opacity = "0";
}

Each step now has a delay based on index. This adjusts opacity correctly over time.