Make Javascript (scheduler?) wait until a Task object is done repeating
//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!
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.
"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!
For those in the future, this was the general strategy that worked for "pausing" my code until a needed Task function was complete.
Change variables that need to be shared (or not lost when the process function stops) to global variables
Create an initializer function that resets those global variables to the starting value and then kicks off the desired process function.
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.
When the desired condition is found,
A global counter is set to 0
The task function is called for a given amount of repetitions
A boolean flag is set that allows the function to pause using break or return commands nested in if statements checking for that flag
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!