Friday 15 August 2014

javascript - setTimeout passing arguments issue -


I am trying to slide up the contents of an index and the contents of each list item with delays

Here (+ # ColContainer li). Length; I ++) My code for {var el = $ ("# colContainer li: nth-1 = $") is

 . Child ("+ i +") .colContent "); Var delay = function () {slide (L);}} SetTimeout (delay, 10); function slide (L) {el.slideUp ();};};   

But every time just makes a last slide

What do I expect from those slides from the late 1 to the end

I also Tried to do this

  index = $ (this) .Parent ("li") for index (); (var i = 1; i & lt; = $ ("# ColContainer li ") Length; i ++) {(function (i) {var el = $ (" # colContainer li: nth-child ("+ i +") .colContent "); var delay = function () {slide (L);} Setimeth (Will B, 10); function slide (L) {el.slideUp ();};}) (i);};   

But they all slide once, let me index 2 After index 2 and ...

Is there no way to loop?

< P> You need to close the el scope for each iteration of the loop. for (var i = 1; i & lt; = $ ("# colContainer li") Length; i ++) {var el = $ ("# colContainer li: nth-child (" + i + ") colContent"); (Function (L) {setTimeout (function () {el.slideUp ();}, 10);}) (L); }

Although it will still animate all of them at the same time because if this is the desired result, you can do it in the same step with jQuery. If you want you to animate them at one time, then you can:

 for  (var i = 1; i & lt; = $ ("# colContainer li" ); Length; i ++) {(function (i) {var el = $ ("# colContainer li: nth-child (" + i + ") .colcontent"); setTimeout (function () {el.slideUp ( );}, I * 10);}) (i); }    

No comments:

Post a Comment