onMeshImportedObservable Debounce


I’m trying to detect when the meshes are changing in my scene, and I would like to add a debounce in order to reduce the calls.

this.scene.onMeshImportedObservable.add(() => {});

Do you know how I can get the Observable from Babylon.Observable?


Hey Rafael…not quite sure to understand the question :frowning:
What do you need? the Observable class? the instance? a link to the code?

Good question :slight_smile:

There are a few ways to do that using babylon’s observables. The simplest way would be to add a new callback (with insertFirst set to true), and checking in this callback how often it is called. then set the state object to skip the next calls. Without testing too much, this is what I would do:

let lastCalled = new Date().getTime();

this.scene.onMeshImportedObservable.add((_, state) => {
    let now = new Date().getTime();
    if(now - lastCalled < 500 /* half a second */) {
        // skip next calls
        state.skipNextObservers = true;
    } else {
        lastCalled = now;
}, -1, true);

Basically, I download a model, and I add multiples meshes. I need to know when it is finished!

Screenshot 2019-11-22 at 15.26.19

For example, I have a class to manages a grid! I don’t want to take care about upload the grid manually, for that reason, just I add the scene to the GridManager class, and everytime a new mesh is added or removed, I update the bounding box, … and the grid! :slight_smile:

If add 100 meshes, it is updating any time, for that reason, I was thinking to add a Debounce, to manage that, only executes after 500 ms.

I’m trying to use the RaananW! I will keep informed!

By other hand, I love BabylonJS! And the community!! And you!!! … And the youtube channel too!!

Thank you guys!!



We love you too :wink:

1 Like


I was trying your example, but I can not do it. I created a simple playground. I add 100 spheres, I need to know when is not adding more spheres after 500ms.


Does it make sense?


Is this the way you expect it to work?


meshes are added every 100 ms, but the observer(s) are called only every 500 ms. a kind of debounce.

It can works! But the only problem is, if you just add 9 spheres? it just call in the number 5, right?

everything in this example can be changed. I also use setInterval, which will simply continue until you reload or restart.

It is just to show how something like debounce can be developed using babylon’s observables

I see! :slight_smile: Thank you for your help!