TypeScript: Abort or cancel a scheduled function

schedule and scheduleRepeating return Dispose object which it meant for such use cases (docs).

For example for the second part of your question you can use something like that:

class PlatformNotificator {
    private disposables: Map<String, Disposable> = new Map()

    onCollisionEnter(collider: BaseItem): void {
        let disposable = Time.schedule(() => {
            Debug.log("Stop standing on me!");
        }, 5);
        this.disposables[collider.id] = disposable

    }

    onCollisionExit(collider: BaseItem): void {
        let disposable = this.disposables[collider.id]
        if (disposable) {
            disposable.dispose()
            this.disposables.delete(collider.id)
        }
    }
} 
let notificator = new PlatformNotificator()
timedPlatform.onCollisionEnter(notificator.onCollisionEnter);
timedPlatform.onCollisionExit(notificator.onCollisionExit);

Note, that I didn’t test it, but this should give you an idea.

For the first part I would recommend you to take a look at observer pattern. Using scheduleRepeating for checking state change is error prone, inefficient and not extensible.

3 Likes