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

    Javascript

    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
      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!
    • Trevor being Trevor's icon
      Trevor being Trevor's icon
      Trevor being Trevor
      Apr 05 2023 | 7:43 pm
      For those in the future, this was the general strategy that worked for "pausing" my code until a needed Task function was complete.
      1. Change variables that need to be shared (or not lost when the process function stops) to global variables
      2. Create an initializer function that resets those global variables to the starting value and then kicks off the desired process function.
      3. The desired processing function searches for a desired condition to be true within a data set using a loop to process each piece of data. Each iteration increments the necessary global variables.
      4. When the desired condition is found,
        1. A global counter is set to 0
        2. The task function is called for a given amount of repetitions
        3. A boolean flag is set that allows the function to pause using break or return commands nested in if statements checking for that flag
      5. The Task function runs, incrementing the global counter each time. When it reaches it's final repetition (use the global counter to check), it call the process function again. The process function picks up where it left off using the global variables.
      Hopefully that helps!