3 Replies Latest reply on Mar 7, 2014 11:32 PM by hemanthR

    loop code x times with counting parameter / variable

    cyclux

      Hi there,

      due to my lack of knowledge about coding JavaScript, I'm stuck here and I can't find the right way by google and try and error..

       

      I want to loop a code / function .. let's say 100 times and each time one parameter or variable is counted up.

      I found "for"-loops and some with a counter.. but I coudn't get it to work in Edge Animate..

       

      Here is a fragment of the code I want to loop ->

       

       

      $.get("http://......./publish/web/output1.php?id="+Num, {}, function(ask, code) {        

        

      switch(ask) {

      .....

              case '9':

          sym.$("leaf"+Num).show();

          var leaf = sym.$("leaf"+Num);   // How can I count the setting variable? "var leaf+Num = ... " doesn't work I guess..

          TweenMax.to(leaf+Num, 1, {scale:1.5});

          TweenMax.to(leaf+Num, 2, {rotation:rand, ease:"Power1.easeInOut", yoyo:true, repeat:-1});

          sym.randfunc();

        break;

          }

      });

       

       

      Thanks for help!

        • 1. Re: loop code x times with counting parameter / variable
          hemanthR Adobe Employee

          My guess is your are running into closure scoping issue of JS in your callback function like the one mentioned here

          http://stackoverflow.com/questions/3572480/please-explain-the-use-of-javascript-closures-i n-loops

          See if this work for you

          $.get("http://......./publish/web/output1.php?id="+Num, {},

          (function(Num){

          return function(ask, code) {       

           

          switch(ask) {

          .....

                  case '9':

              sym.$("leaf"+Num).show();

              var leaf = sym.$("leaf"+Num);   // How can I count the setting variable? "var leaf+Num = ... " doesn't work I guess..

              TweenMax.to(leaf+Num, 1, {scale:1.5});

              TweenMax.to(leaf+Num, 2, {rotation:rand, ease:"Power1.easeInOut", yoyo:true, repeat:-1});

              sym.randfunc();

            break;

              }

          }

          })(Num)

          );

          1 person found this helpful
          • 2. Re: loop code x times with counting parameter / variable
            cyclux Level 1

            Thanks a lot rhemanthkumar! You pointed in the right direction..

             

            This is my code now..It works fine, but if you see potential for optimization please tell me

             

             

             

            for (i=0;i<150;i++){

             

            $.get("http://127.0.0.1/wordpress/tree/publish/web/output1.php?id="+i, {},

             

            (function(i){

             

            return function(ask, code) {        

             

              switch(ask) {

                    case '0':

                        sym.$("leaf"+i).hide();

                sym.randfunc();

            break;

                    case '1':

                sym.$("leaf"+i).show();

                    TweenMax.to(sym.$("leaf"+i), 1, {scale:1});

                TweenMax.to(sym.$("leaf"+i), 2.5, {rotation:rand, ease:"Power1.easeInOut", yoyo:true, repeat:-1});

                sym.randfunc();

            break;

                    case '2':

                sym.$("leaf"+i).show();

                    TweenMax.to(sym.$("leaf"+i), 1, {scale:1.25});

                TweenMax.to(sym.$("leaf"+i), 2.5, {rotation:rand, ease:"Power1.easeInOut", yoyo:true, repeat:-1});

                sym.randfunc();

            break;

                    case '3':

                sym.$("leaf"+i).show();

                TweenMax.to(sym.$("leaf"+i), 1, {scale:1.5});

                TweenMax.to(sym.$("leaf"+i), 2.5, {rotation:rand, ease:"Power1.easeInOut", yoyo:true, repeat:-1});

                sym.randfunc();

             

                 break;

                }

            }

             

            })(i)

            );

            }

            • 3. Re: loop code x times with counting parameter / variable
              hemanthR Adobe Employee

              Sorry will not be able to help yout out much in optimizing the above code as I am not much of a JS expert and more over no sure of in what context your code being run.