Make Javascript (scheduler?) wait until a Task object is done repeating

    Javascript

    Trevor being Trevor's icon
    Trevor being Trevor's icon
    Trevor being Trevor
    Mar 18 2023 | 6:31 am
    //Some half Pseudocode that hopefully makes sense of my problem
    var isNotDone = true;
    var waitCounter
    tsk = new Task(repeater_function, this)
    tsk.interval = 100;
    
    function repeater_function(){
        //do a task eqivalent to sending a note value out an outlet
        waitCounter++
    }
    
    while(isNotDone){
        *** a bunch of code ***
        
        if(certain condition is true){
            tsk.repeat(20);
            
            while(waitCounter < 20){
            // do not pass go, do not collect 200 dollars,
    	// i.e. do nothing until tsk.repeat(20) is done
            }
        }
        
        ***more code below***
    }
    I need to delay a series of events, hence the task object, but I also need to make sure the rest of the code waits until that series of events is complete to move on to the code below. Is there a way to do that without creating an infinite while loop as I have done above? I can't seem to find a way to accomplish this at least without a fundamental rebuild of how the rest the code is structured.
    I am willing to use other methods such as Max Externals, but I need to access the Live API. As always, any advice is greatly appreciated!

    • tyler mazaika's icon
      tyler mazaika's icon
      tyler mazaika
      Mar 18 2023 | 8:02 am
      A little tough to answer from the pseudo code since no where do you set the conditions which terminate either of the while loops.
      But mainly I think the troubling thing is that in your outer while loop, executing the code inside the “if” doesnt block execution of the “more code below” in the first iteration.
      Scheduling/running the task makes the task code asynchronous, and evaluation of the parent context will continue regardless of the task condition/evaluation.
      so put the evaluation of the counting (rather than just the incrementing) inside the task callback. and when the task has been called 20 times, call the ***more code below*** from within the task (or by a function call inside task callback), and cancel the task. I usually use repeated individual calls to task.schedule(), which you can re-call from within the task callback itself if needed, rather than repeat(), fwiw. I think for indeterminate duration operations it works better. Also, Rather than a bool isNotDone you could have an integer for the critical stages: 0 (not running) 1 (initial condition set) 2 (waiting using task) 3 (task is complete)
      it would add more clarity to understanding the overall control flow process.
      Share
    • Trevor being Trevor's icon
      Trevor being Trevor's icon
      Trevor being Trevor
      Mar 19 2023 | 12:57 am
      "Scheduling/running the task makes the task code asynchronous, and evaluation of the parent context will continue regardless of the task condition/evaluation."
      Okay that makes a ton of sense. I have some ideas on how to call the next iteration within task function now. Thanks! I will add more detail to my Pseudocode just to clarify and 'fix' the other things you mentioned.
      //Some half Pseduocode V2
      //this is still simplified, but hopefully allows us to focuses on the problem better
      
      var waitCounter = 0;
      tsk = new Task(repeater_function, this)
      tsk.interval = 100;
      
      function repeater_function(){
          //do a task eqivalent to sending a note value out an outlet
          waitCounter++
      }
      
      //the main goal of this loop is look through a list for a set of matches
      //and then do something if it finds a match
      //I included this while loop in the pseudocode because it makes
      //makes calling the code that needs to come after it from within the task function
      //not as easy of switch up. I wondered if I needed to something like this
      //but wasn't sure until tyler helped clarify some things. Plus the code,
      //was working pretty efficiently otherwise so I didn't want to give it up.
      //Appreciate the help
       
      while(There are still list items to check && there are still things to check for){
          
          //begin checking each item in the list for the first item i am looking for
          
          if(a match in the list is found){
              waitCounter = 0;
              tsk.repeat(20);
              
              while(waitCounter < 20){
              // do not pass go, do not collect 200 dollars,
      	// i.e. do nothing until tsk.repeat(20) is done
              }
          }
          
          //increment a counter to start looking for the next thing
      }