MZ Wait for script to complete

Hi,
So I want to add a little minigame to my rpg, so I wrote a script for it. The script works pretty well except I'm having trouble with getting the delay to work right. I put a minimal example in the picture below. As in the picture, the structure of the event I want in rpg maker mz is:
(1) ask the player for something (that works fine) then if yes:
(2) play a mini-game. this mini-game is a script that is much more complicated than the below script (uses PIXI etc.) but it does work.
(3) then AFTER the mini-game is complete, talk some more
The problem I'm having is that the event does not "wait" for the minigame to end. Like in the below minimal example, in my minigame I did a loop by having a counter then recursively calling a function f() with setTimeout(). That works fine for the game itself, but the event just seems to continue on to the text after that even while my mini-game is still playing! (in the below example, my mini game is counting from 1 to 10, so not so much of a game haha. But it gets the idea across.)
So here's my big question: what do I need to do so that this event does not continue until AFTER the script in the middle there is done? Is there something I can call that's like "stop execution" at the beginning of the script, then in the 10th counter can I call "continue execution" somehow?
Any help is super appreciated -- thanks!!
1639890460483.png
Since you want a loop, what about you just use the RPG Maker eventing loop for that and put your code in between? Do note to put wait for 1 frame for each script call to prevent a lock.
@TheoAllen Thanks. What do you mean by "put wait for 1 frame for each script call"?

It's an interesting idea, to use the rpgmaker loop construct and then put each iteration of the mini-game inside the loop. However, wouldn't looping that way delete all the javascript variables in the mini-game on each iteration? Though maybe I could work around that by storing things in $gameVariables.

It seems like there should be a way to do what the rpg maker eventing loop does in the js code.
This!

dJ2abz4.png
However, wouldn't looping that way delete all the javascript variables in the mini-game on each iteration? Though maybe I could work around that by storing things in $gameVariables.
You can use $gameVariables or initialize your own variable in the global window. For example window["$yourVarName"] = 0
"put wait for 1 frame for each script call"
your problems come from the fact that the wait command does not work like you assume it does. It will only look like waiting in case of a triggered event, but in reality it tells the main game loop to skip processing for a number of frames.

the mai game loop cannot be halted or your game would freeze, which means you'll have to sync by other means to make the player think there is a wait. And that is what the tips above are about.
might be a clunky solution but you could have your event require a switch to be on to run and have the switch flipped at the end of the minigame.
Thanks for everyone's help! The following works:

1639933935761.png

This comes from combining your comments together above. So the idea is that we know the main game loop is going to continue regardless of the script, because that's the way setTimeout() works. So we need to do something to hold the main game loop. One way to do this is to add a event loop right after the script that checks every frame for a switch that is flipped inside the script. Only if the switch is 1 do we let the main game loop continue.
The Wait : 1 frame is really important because otherwise the game soaks up all resources spinning in that loop. If I drop the Wait : 1 frame, the browser window does not have the resources to run my mini game and it lags like crazy! (By the way, there's no performance benefit really for Wait : 2 frame or anything higher right? 1 frame (1/60 sec) is already pretty long I think.)

This approach also nicely keeps the BGM playing during the mini game.

So far I have not found any problems with this approach. However, I would very much appreciate any additional feedback you may have! I would especially be interested if there's any way to do this in the js script directly. Although this approach seems perfectly usable -- just have to copy & paste that "hold" loop around everywhere after mini games -- are there ways to call event loops inside the js script directly? Like some $game... variable that could be called, maybe?

And here's a gif of my first draft of the mini game -- player has to press trigger when the dot is at the highest point of the stock chart. Very rough at this point, but super cool that it appears to work now. Thanks everyone!

ezgif-4-4a56d4028d.gif

Original thread: https://forums.rpgmakerweb.com/threads/wait-for-script-to-complete.143132/