3 Replies Latest reply on Feb 27, 2015 7:22 PM by W_J_T

    Nested functions not waiting

    Zantcor
      var confirmed = confirm ("You picked [ " + selected_rbutton (totalgroups) +" ]"+ "\nYou picked [ " + questionResize (rsizechoice) + " ] to resize" + "\n" +"\nContinue?");
              confirm.noAsDflt == false;
              
              if (confirmed == true)
              {
                  copy();
                  openTemp ();
                  paste();
                  centerArt ();
                  resizeart ();
                  savefile ()
                  putname ();
                  
               } /*end if */ else
           {
               alert ("You chose not to continue, script stopped")
               }//end else
      }//end if OK
      

       

      I've coded everything inside separate functions this way I can save space letting the user make a choice and each choice will use different groups of functions.  My only problem is that the some of the functions are still doing their thing when the next starts but it may depend on the one above it to finish.  Function savefile () lets the user type in a new name and it saves to a couple different folder then Function putname () puts that users name and file name at the top of the artboard in the corner.  What is happening is that the user is chooseing to rename the file but the Function putname () has already filled its variables with the old name not the new one.  How can I make it wait till function savefile () is done?

        • 1. Re: Nested functions not waiting
          CarlosCanto Adobe Community Professional & MVP

          putname() should not start before savefile() finishes, if necessary, one way to make it wait would be

           

          var wait = false;
          var a = 0;
          
          
          wait = savefile();
          
          
          while(!wait) {
          
          
              //wait = false; // this is just killing time
              a++; // so is this  
              
              if (a>1000) break; // guard against infinite loop
          }
          
          
          putname();
          
          
          function savefile() {
              // do your thing
          
          
              return true;
          }
          
          
          function putname() {
              alert('finally!');
          }
          
          • 2. Re: Nested functions not waiting
            Zantcor Level 1

            Thanks Carlos, the function putname() now waits for the file to be saved then it puts all the information the artboard.

            • 3. Re: Nested functions not waiting
              W_J_T Level 4

              From your other thread and in relationship to this thread, below is how I would suggest approaching what you stated of running functions in sequence:

              Re: Script errors and a question

               

              Zantcor wrote:


              All I need it to do is the following in the correct order;

               

              copy();

              openTemp ();

              paste();

              centerArt ();

              resizeart ();

              savefile ()

              putname ();

               

               

              Two Possible Approaches Shown Below: [shown in basic simple form, for these examples]


              1. ] Passing Functions through Functions:

              The first approach is make each function modular, allowing it to pass and call other functions if and when needed. You can do this by passing in a parameter to the function of a function. Then evaluate the parameter and respond accordingly. Below is a basic example of how this can be done.

               

              /*
              
              Example of calling successive functions by passing a new function through each function
              
              */
              function sampleInit() {
                // Example of calling successive functions using this approach
                  alert("Invoked from:\nsampleInit()");
                  sampleFunction1(sampleFunction2);
                  sampleFunction3(sampleFunction4);
                  sampleFunction5();
              };
              sampleInit();
              //
              function sampleFunction1(fName) {
                  // All the code that is evoked with this function
                  var sampleArray = [];
                  for (var i = 0, l = 100; i < l; i++) {
                      sampleArray.push("1");
                  }
                  alert("Invoked from:\nsampleFunction1()\n\n"+sampleArray);
                  // End of all the code for this function
                  // Then at the end call another function if passed or if null, do nothing.
                  if (fName && (typeof fName == "function")) {
                      fName();
                  }
              }
              function sampleFunction2(fName) {
                  var sampleArray = [];
                  for (var i = 0, l = 200; i < l; i++) {
                      sampleArray.push("2");
                  }
                  alert("Invoked from:\nsampleFunction2()\n\n"+sampleArray);
                  if (fName && (typeof fName == "function")) {
                      fName();
                  }
              }
              function sampleFunction3(fName) {
                  var sampleArray = [];
                  for (var i = 0, l = 300; i < l; i++) {
                      sampleArray.push("3");
                  }
                  alert("Invoked from:\nsampleFunction3()\n\n"+sampleArray);
                  if (fName && (typeof fName == "function")) {
                      fName();
                  }
              }
              function sampleFunction4(fName) {
                  var sampleArray = [];
                  for (var i = 0, l = 400; i < l; i++) {
                      sampleArray.push("4");
                  }
                  alert("Invoked from:\nsampleFunction4()\n\n"+sampleArray);
                  if (fName && (typeof fName == "function")) {
                      fName();
                  }
              }
              function sampleFunction5(fName) {
                  var sampleArray = [];
                  for (var i = 0, l = 500; i < l; i++) {
                      sampleArray.push("5");
                  }
                  alert("Invoked from:\nsampleFunction5()\n\n"+sampleArray);
                  if (fName && (typeof fName == "function")) {
                      fName();
                  }
              }
              
              

               

              2. ] Array of Functions:

              The next example uses an array to call the functions without passing the function into and through each other (it's basically the same setup as above, just stripped of that feature).

               

              /*
              
              Example of calling successive functions using an array
              
              */
              function sampleInit() {
                  alert("Invoked from:\nsampleInit()");
                  var functionArray = [sampleFunction1, sampleFunction2, sampleFunction3, sampleFunction4, sampleFunction5];
                  for (var i = 0, l = functionArray.length; i < l; i++) {
                      functionArray[i]();
                  }
              };
              sampleInit();
              //
              function sampleFunction1() {
                  var sampleArray = [];
                  for (var i = 0, l = 100; i < l; i++) {
                      sampleArray.push("1");
                  }
                  alert("Invoked from:\nsampleFunction1()\n\n"+sampleArray);
              }
              function sampleFunction2() {
                  var sampleArray = [];
                  for (var i = 0, l = 200; i < l; i++) {
                      sampleArray.push("2");
                  }
                  alert("Invoked from:\nsampleFunction2()\n\n"+sampleArray);
              }
              function sampleFunction3() {
                  var sampleArray = [];
                  for (var i = 0, l = 300; i < l; i++) {
                      sampleArray.push("3");
                  }
                  alert("Invoked from:\nsampleFunction3()\n\n"+sampleArray);
              }
              function sampleFunction4() {
                  var sampleArray = [];
                  for (var i = 0, l = 400; i < l; i++) {
                      sampleArray.push("4");
                  }
                  alert("Invoked from:\nsampleFunction4()\n\n"+sampleArray);
              }
              function sampleFunction5() {
                  var sampleArray = [];
                  for (var i = 0, l = 500; i < l; i++) {
                      sampleArray.push("5");
                  }
                  alert("Invoked from:\nsampleFunction5()\n\n"+sampleArray);
              }
              
              

               

              Sorry for the delay, after talking to you in your other thread, I started to post here then got busy with something else. Anyway, hopefully one of these approaches help you organize and execute your functions as desired.

               

              What you think Carlos, can you share some correct answer points?