Cancel the function for onAnimationEndObservable.addOnce

I have a function added for onAnimationEndObservable.addOnce(() => {})

I found this function always execute, even I switch to another animation group to play or stop it,
Is there any way to stop the onAnimationEndObservable.addOnce execute?

I created a playground here, AnimationGroup demo | Babylon.js Playground (babylonjs.com)

I tried to remove observer when animation stops, but it does not work, the console.log(1) always print out for the first time since I only use addOnce

Thanks in advance!


Update:

I find change the order of the code just make it work, still not sure what’s the reason, if anyone could explain, thanks

working with change order: AnimationGroup demo | Babylon.js Playground (babylonjs.com)

working with comment out animation stop: AnimationGroup demo | Babylon.js Playground (babylonjs.com)

How about Observable.remove and similar, related functions?

More info here:

1 Like

Thank you, I tried to create the method you mentioned in the above playground, but it does not work as expected, did I do something wrong?

I’m a bit confused as to what you wanted to happen.

From the Playground, everything looks to be as I would expect? The observable callback is triggered only once. If you remove it before it can be triggered (the animation ends), it will not be triggered. If you remove it after the animation has ended, the callback will already be queued and run regardless since that’s how JavaScript works.

A high-level description of how messages are queued can be found here: The event loop - JavaScript | MDN

Or maybe you’re asking something else entirely?

3 Likes

Thank you, I think you just answered my question, thank you for the observer links and your explanation again :slight_smile: