Home > Net >  How can I decrease the opacity of an element by 0.1 every 100 milliseconds?
How can I decrease the opacity of an element by 0.1 every 100 milliseconds?

Time:10-26

I am trying to make it so every 100 milliseconds, the opacity of a specified element decreases by 0.1. Here's my code:

       var loader = this.document.getElementById("loader");
       var op;
       for(op = 1.0; op > 0.0; op -= 0.1){
          window.setTimeout(function(){
              c.style.opacity = op.toString();
              loader.style.opacity = op.toString();
          }, 100)
       }

Am I potentially creating an infinite loop that I'm not aware of?

CodePudding user response:

An easy way to do this would be with the setInterval("yourFunctionHere()", 100) This will make the function you add run in the background and execute every 100 milliseconds. Just put your set opacity stuff into a function and execute it in the setInterval

  • Related