10 Replies Latest reply on Jan 19, 2010 7:23 AM by kglad

    Calling a javascript function invinite loop - Help

    Sprogleg Level 1

      I'm trying to get AS to call a javascript function. This is working fine. As a test a changed the js function to trigger an alert.

       

      This works and the alert comes up but it forms a loop and I'm not sure you to get it to stop repeating the alert.

       

      You can see what I mean at http://test.justsandymedia.com/working_15_01_10/working_15_01_10v5_1.html

       

      ---------------WARNING THIS LINK WILL OPEN AN SWF WHICH WILL CREATE A LOOP WHEN IT CLOSES. YOU WILL HAVE TO FORCE QUIT YOUR BROWSER SO MAKE SURE THAT YOU DON'T HAVE ANYTHING IMPORTANT UNSAVED OPEN----------

       

      Sorry for the caps but I want to make sure everyone knows what will happen.

       

      Here is my AS

      // Create a movie clip instance.
      import flash.external.*;
      
      var my_fmt:TextFormat = new TextFormat();
      my_fmt.color = _root.textcolor;
      my_fmt.size = _root.textsize;
      my_fmt.font = _root.textfont;
      closebut_mc.closetext_txt.text = _root.closetext;
      adbut_mc.adtext_txt.text = _root.adtext;
      closebut_mc.closetext_txt.setTextFormat(my_fmt);
      adbut_mc.adtext_txt.setTextFormat(my_fmt);
      
      
      import flash.geom.ColorTransform;
      bckgrnd_mc.onEnterFrame = function() {
          var colorTransform:ColorTransform = this.transform.colorTransform;
          colorTransform.rgb = _root.bgcolor;
          this.transform.colorTransform = colorTransform;
      }
      
      this.createEmptyMovieClip("img1_mc", 10);
      var mcl_obj:Object = new Object();
      mcl_obj.onLoadInit = function (target_mc:MovieClip):Void {
          function closebanner(){
              target_mc.onEnterFrame = function() {
              target_mc._y += 10;
              bckgrnd_mc._y += 10;
              click_btn._y = 150;
              closebut_mc._y += 10;
              adbut_mc._y += 10;
              ExternalInterface.call("pgcover");
              stop();
              };
              
      }
          target_mc._y = 110;
          target_mc._x = 94;
          target_mc.onEnterFrame = function() {
              target_mc._y -= 10; // decrease current _y position by 10 pixels
              if (target_mc._y <= 0) {
                  target_mc._y = 0;
                  delete target_mc.onEnterFrame;
                  setTimeout(closebanner,_root.screentime); //5 seconds
                  setTimeout(closefade,_root.screentime); //5 seconds
                  stop();
              }
          };
      };
      var img_mcl:MovieClipLoader = new MovieClipLoader();
      img_mcl.addListener(mcl_obj);
      // Load an image into the movie clip
      //_global.bannerImage is a global var set to be a var which is passed from JS on html page
      img_mcl.loadClip(_root.bannerImage, img1_mc);
      
      
      
      _root.closebut_mc.closebut_btn.onRelease = function(){
          img1_mc._y = 110;
          bckgrnd_mc._y = 150;
          click_btn._y = 150;
          closebut_mc._y = 150;
          adbut_mc._y = 150;
          ExternalInterface.call("pgcover");
      }
      
      _root.adbut_mc.adbut_btn.onRelease = function(){
          getURL (_root.adclicktag, "_blank");
      }
      
      _root.closebut_mc.swapDepths(getNextHighestDepth());
      _root.adbut_mc.swapDepths(getNextHighestDepth());
      
      click_btn.onRelease = function(){
          getURL (_root.clicktag, "_blank");
      }
      
      //getURL("javascript:displayPost(" + postId + "," + feedId +")");
      // the above line of code(74) may also be used possibly to call javascript functions where displayPost
      // is the javascript function to be called.
      
      
      

       

      And here is my html/js

      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>working_15_01_10v5_1</title>
      </head>
      <body bgcolor="#FFFFFF">
      <!--url's used in the movie-->
      <!--text used in the movie-->
      <!-- saved from url=(0013)about:internet -->
      
      <script type="text/javascript">
      function pgcover() {
      alert ('pgcover');
      }
      </script>
      
      <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="1015" height="110" id="working_15_01_10v5_1" align="middle">
      <param name="allowScriptAccess" value="sameDomain" />
      <param name="movie" value="working_15_01_10v5_1.swf" /><param name="quality" value="high" />
      
      <PARAM NAME="FlashVars" VALUE="bannerImage=banner2%2Epng&clicktag=http://www.building.co.uk&bgcolor=#ff0000&screentime=5000&textcolor=0xFFDDFF&textsize=15&textfont=Verdana&closetext=close&adtext=advertise here&adclicktag=http://www.building.co.uk/hybrid.asp?navcode=2437">
      
      <param name="wmode" value="transparent" /><param name="bgcolor" value="#FFFFFF" />
      <embed src="working_15_01_10v5_1.swf" quality="high" wmode="transparent" bgcolor="#FFFFFF" width="1015" height="110" name="working_15_01_10v5_1" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="bannerImage=banner2%2Epng&clicktag=http://www.building.co.uk&bgcolor=0xff0000&screentime=5000&textcolor=0xFFDDFF&textsize=15&textfont=Verdana&closetext=close&adtext=advertise here&adclicktag=http://www.building.co.uk/hybrid.asp?navcode=2437" />
      </object>
      </body>
      </html>
      
      

       

      Any help would be great, thanks.

        • 1. Re: Calling a javascript function invinite loop - Help
          kglad Adobe Community Professional & MVP

          1.  never nest named functions.  ie, remove closebanner() from onLoadInit().

           

          2.  remove the onEnterFrame loops.  (what are you trying to do?)

          • 2. Re: Calling a javascript function invinite loop - Help
            Sprogleg Level 1

            I nested the closebanner function as I couldn't find another way to get it to work. How can retain the functionality with it un-nested?

             

            If I remove the onenterframe loops then the entire swf will fail.

             

            In the simplest terms I'm trying to get it to do this http://test.justsandymedia.com/working_15_01_10/working_15_01_10v5.html

             

            But when it has finished I want to use js to swap in an empty swf so that the content of the original doesnt get in the way.(I'll be honest, I'm being told this from a web dev, who doesn't know anything about flash and I don't know much about js).

            • 3. Re: Calling a javascript function invinite loop - Help
              kglad Adobe Community Professional & MVP

              i don't want to check your link if it's going to crash/freeze my browser.

               

              pass the load-target to your nested function.

               

              addendum:  you're not even using that moviecliploader in the code you're showing.

              • 4. Re: Calling a javascript function invinite loop - Help
                Sprogleg Level 1

                the top link is the one with the loop. It doesnt crash just creates a loop

                which keeps calling an alert function.

                 

                The 2nd link is before I added the js alert function, which is just there to explain what I want to do.

                • 5. Re: Calling a javascript function invinite loop - Help
                  Sprogleg Level 1

                  As much as I would love to understand your reply, I don't.

                  • 6. Re: Calling a javascript function invinite loop - Help
                    kglad Adobe Community Professional & MVP

                    i checked your 2nd link.  what's the relationship between the shown tween and javascript?  ie, why would you repeatedly call a javascript function?

                    • 7. Re: Calling a javascript function invinite loop - Help
                      Sprogleg Level 1

                      I have the AS set to call a JS function after the banner fades back down or when the close button is clicked.

                       

                      I set the js to an alert function to test that flash was calling it. I then noticed that it was repeating.

                       

                      It is meant in the finished version call a js function to change the css on a div(change the top value).

                       

                      Sorry if none of this makes sense, I'm not very competent on either flash or js.

                      • 8. Re: Calling a javascript function invinite loop - Help
                        kglad Adobe Community Professional & MVP

                        i see another onEnterFrame loop that doesn't make sense in your code.

                         

                        do you realize that onEnterFrame functions repeatedly execute (several times per second) until the loop is deleted?

                        • 9. Re: Calling a javascript function invinite loop - Help
                          Sprogleg Level 1

                          I'm trying to sort the code out. Tidy it up and get rid of as much mess as possible.

                           

                          I've changed to this code now but it doesn't seem to want to reverse the animation.

                           

                          I've put in a couple of traces and it seems to be getting to the code just not doing anything.

                           

                          I'm aware that onEnterFrame calls repeatedly but I'm a little rusty as I haven't used flash in a while. Thanks for bearing with me.

                           

                          Here is my new code:

                          import flash.external.*;
                          
                          var my_fmt:TextFormat = new TextFormat();
                          my_fmt.color = _root.textcolor;
                          my_fmt.size = _root.textsize;
                          my_fmt.font = _root.textfont;
                          closebut_mc.closetext_txt.text = _root.closetext;
                          adbut_mc.adtext_txt.text = _root.adtext;
                          closebut_mc.closetext_txt.setTextFormat(my_fmt);
                          adbut_mc.adtext_txt.setTextFormat(my_fmt);
                          
                          
                          import flash.geom.ColorTransform;
                          bckgrnd_mc.onEnterFrame = function() {
                              var colorTransform:ColorTransform = this.transform.colorTransform;
                              colorTransform.rgb = _root.bgcolor;
                              this.transform.colorTransform = colorTransform;
                          }
                          
                          
                                  
                          // Create a movie clip instance.        
                          this.createEmptyMovieClip("img1_mc", 10);
                          var mcl_obj:Object = new Object();
                          mcl_obj.onLoadInit = function (target_mc:MovieClip):Void {
                              target_mc._y = 110;
                              target_mc._x = 94;
                              target_mc.onEnterFrame = function() {
                                  target_mc._y -= 10; // decrease current _y position by 10 pixels
                                  if (target_mc._y <= 0) {
                                      target_mc._y = 0;
                                      
                                      //SetInterval calls the function reverseAnim after .5 seconds
                                      timerID = setInterval(reverseAnim, 500);
                                      delete target_mc.onEnterFrame;
                                      }
                              };
                          };
                          var img_mcl:MovieClipLoader = new MovieClipLoader();
                          img_mcl.addListener(mcl_obj);
                          // Load an image into the movie clip
                          //_global.bannerImage is a global var set to be a var which is passed from JS on html page
                          //img_mcl.loadClip(_root.bannerImage, img1_mc);
                          img_mcl.loadClip("banner.png", img1_mc);
                          
                          function reverseAnim(){
                              
                              target_mc.onEnterFrame = function(){
                                  target_mc._y += 10; //increase current _y position by 10 pixels
                                  if (target_mc._y > 110){
                                      target_mc._y = 110;
                                      clearInterval(timerID);
                                      delete target_mc.onEnterFrame;
                                      
                                  }
                              };
                          }
                          
                          _root.closebut_mc.closebut_btn.onRelease = function(){
                              img1_mc._y = 110;
                              bckgrnd_mc._y = 150;
                              click_btn._y = 150;
                              closebut_mc._y = 150;
                              adbut_mc._y = 150;
                              ExternalInterface.call("pgcover");
                          }
                          
                          _root.adbut_mc.adbut_btn.onRelease = function(){
                              getURL (_root.adclicktag, "_blank");
                          }
                          
                          _root.closebut_mc.swapDepths(getNextHighestDepth());
                          _root.adbut_mc.swapDepths(getNextHighestDepth());
                          
                          click_btn.onRelease = function(){
                              getURL (_root.clicktag, "_blank");
                          }
                          
                          • 10. Re: Calling a javascript function invinite loop - Help
                            kglad Adobe Community Professional & MVP

                            1:

                            bckgrnd_mc.onEnterFrame = function() {
                                var colorTransform:ColorTransform = this.transform.colorTransform;
                                colorTransform.rgb = _root.bgcolor;
                                this.transform.colorTransform = colorTransform;
                            }

                             

                             

                            should not be in a loop.  if you change _root.bgcolor periodically and you want to update bckgrnd_mc's color you can call changeColorF while passing the parameters you want.  in the above code you would use changeColorF(bckgrnd_mc,_root.bgcolor):

                             

                            function changeColorF(mc:MovieClip,color:Number){

                            var ct:ColorTransform=mc.transform.colorTransform;

                            ct.rgb=color;

                            mc.transform.colorTransform=ct;

                            }

                             

                            2:

                             

                            always use clearInterval() before setting an interval.

                            be careful about setting identically named intervals:

                            your reverseAnim() won't work because target_mc isn't defined when it's called.  also, you don't want to call reverseAnim() repeatedly.  call it once. use:


                                target_mc.onEnterFrame = function() {
                                    target_mc._y -= 10; // decrease current _y position by 10 pixels
                                    if (target_mc._y <= 0) {
                                        target_mc._y = 0;
                                       
                                        //SetInterval calls the function reverseAnim after .5 seconds
                            clearInterval(this.timerID);
                                        this.timerID = setTimeout(reverseAnim, 500,this);
                                        delete this.onEnterFrame;
                                        }
                                };


                             

                            function reverseAnim(mc:MovieClip){
                               
                               mc.onEnterFrame = function(){
                                    mc._y += 10; //increase current _y position by 10 pixels
                                    if (mc._y > 110){
                                        mc._y = 110;
                                        clearInterval(mc.timerID);
                                        delete this.onEnterFrame;
                                       
                                    }
                                };
                            }
                            1 person found this helpful