Search code examples
javascriptevent-handlingprimitivecesiumjs

How to stop a scheduled ontick event on Cesium


I have two buttons on my code which will add and delete primitives when clicked. On the other hand, I have an event listener for onTick method which will get as an input the active primitive and a variable indicating its index, and is supposed to used that primitive with that particular index for the event.

When I did debugging I saw that although the input argument was updated but the parameter used by the event listener were not updated. And I got the error that the object is destroyed.

Do you know how I can update these arguments or stop the previously scheduled events?


Solution

  • onTick is a Cesium Event with an addEventListener function that returns a hook to unsubscribe. You simply call the return value, later, when you want to end your subscription to that event.

    var unsubscribe = viewer.clock.onTick.addEventListener(myCallback);
    
    // ... later ...
    
    // Stop the onTick callback.
    unsubscribe();