How to create fadein effect in JavaScript using for-loop and anonymous function call -


I am making a simple feedin effect using pure javascript. For this, I have tried three methods. M1 is using recursive functionality and is based on the M2 loop and anonymous function and is based on the M3 loop and string based function call . <

Under the code <1> M1 , the code is giving the right output as FEDIN effect.

But the Fidin effect does not work under M2 and

Now the Fidin effect is generated under M3 .

Can someone guide me how to produce Fadin using M2 effect for loop and anonymous function calls < / p>

M1 ------ works perfectly ------

  function starter () {var i = 0; Var el = document.getElementById ("div1"); Fadein (l, i); } Function feedin (L, I) {i = i + 0.01; Seto (L, I); If (i & lt; 1) {setTimeout (function () {fadeIn (L, I);}, 10);}} function set (L, I) {el.style.opacity = i; } Starter ();   

M2 ------ does not work ------

  function starter () { var i = 0; Var el = document.getElementById ("div1"); Fadein (l, i); } Function fadeIn (l, i) {for (i = 0; i & lt; = i; i = i + 0.01) {setTimeout (function () {seto (el, i);}, i * 1000);} } Function Set (L, I) {el.style.opacity = i; Starter ();   

Using JSHint in JSFiddle on M2 , it says - "Do not work within a loop"

M3 ------ Works appropriate ------ jsfiddle.net/TH2dn/9/

  var i = 0; Var el = document.getElementById ("div1"); Function Feeds (for (i = 0; i & lt; = 1; i = i + 0.01) {setTimeout ("set (" + i + ")", i * 1000);}} function set (i ) {el.style.opacity = i;}   

is based on the question abopve

It says - "Do not work inside the loop"

This is good advice, but this is not your main problem.

Your problem is that you are currently using every function 1 second. They will execute in a random order, starting from 1 second Fadein (L) {var go = function (j) {setTimeout (function ()).

(I);} {set (L, j);}, j * 1000);}}; (var i = 0; i & lt; i = i; Pre>

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -