How to stop CoRoutine

let Flash_Whites;
const FlashButton = () => {
    buttonStart.color = "red";
    buttonStart.background = "#00008b";
    Flash_Whites = async () => {
        await Tools.DelayAsync(2500);
        buttonStart.color = "white";
        buttonStart.background = "#9999ff";
        await Tools.DelayAsync(2500);
        console.log("this");
        buttonStart.color = "red";
        buttonStart.background = "#00008b";
    };
    Flash_Whites();
    console.log(FlashWhites);
    Start_Flash = setInterval(() => {
        Flash_Whites();
    }, 5000);
};

`then:

const StopFlash(){
    clearInterval(Start_Flash);
    Flash_Whites = null;
    buttonStart.color = "white";
    buttonStart.background = "#9999ff";
    Random();
};

Null , or abort() don’t seem to work.
Thank’s

Try
yield break

I think you just need to declare Start_Flash up top so it’s in scope. The yield syntax is just for generator functions. Also, your yields are awaiting same time as total interval, so may not produce expected behaviour - it reads like it will set red and white close to simultaneously.

2 Likes

I made this :

Couldn’t get it to stop “That”.

You have promise pending - see console: https://playground.babylonjs.com/#VGZHH3#9

1 Like

So there isn’t an ‘Easy’ way to cancel a coroutine in BJs, but a “return” if statement after every await will cause a breakout at anytime though.
I think I got it:

1 Like

Lol that was in my first answer but I wasn’t sure in this so left only yield break :slight_smile:

I was looking for an external way to stop it, like in Unity: stopCoroutine(Start_Flash) or JS: clearInterval(Stop_Flash); so your answer threw me off coarse, I didn’t know where to put the return. I’m not sure why they didn’t add something like in Unity, maybe in the next upgrade…

You can use clearInterval if you have access to the handle from setInterval.
colorChange | Babylon.js Playground (babylonjs.com)

It won’t cancel a running method with containing awaits (unless you have a breakout state as you have done). For equivalent to unity coroutine have a look at Javascript generators:
* function() {...}
That functionality is already part of ES6 - you can read more on how they work here:
The Basics Of ES6 Generators (davidwalsh.name)

Have a look at the playGame function here as it can handle your scenario - coroutines are only in Babylonj.js 5:
Coroutines | Babylon.js Documentation (babylonjs.com)

2 Likes

@charliesideroad you are actually not using coroutines here but only promises.

@syntheticmagus and @ryantrem added support for coroutines in babylon with support for cancellation and can probably detail a bit more their work here ???

You can actually try it like this: https://playground.babylonjs.com/debug.html#VGZHH3#18

3 Likes

The easiest way to cancel coroutines is using the cancelAllCoroutines call. I’m not 100% sure I understand what your scenario is trying to do, but this API is made available specifically for stopping coroutines.

2 Likes

Reiterating what others have said, your code as shown isn’t using any kind of coroutines, just promise based async functions. In your example code in the post, you use setInterval, in which case calling clearInterval seems reasonable. You could also have the Flash_Whites function just run your code in a loop, and have another mechanism to cancel the async code running in the loop. AbortController from WebAPI is typically used for this.

You could also use an actual Babylon coroutine, and one way of doing the example provided by @sebavan. That is the lower level coroutine system of Babylon.js. You can also use a higher level coroutine layer based on observables, and cancelling that type of coroutine is done with the function @syntheticmagus mentioned. Here is @sebavan’s example modified to use the observable coroutine layer: colorChange | Babylon.js Playground (babylonjs.com)

4 Likes

Hi @charliesideroad just checking in if you have any more questions about this :slight_smile: