import type { AnyToVoidFnSignature } from '../misc/functions' ; see put export class DelayedTask { protected _fn : AnyToVoidFnSignature | null = null ; protected _timeout : number | null = null ; /** Execute deferred task immediately */ protected run = ( ) : void => { this . _timeout = null ; this . _fn && this . _fn ( ) ; } ; returns public get fn ( ) : AnyToVoidFnSignature | null { return this . _fn ; } param task param delay public put ( task , delay = false ) { const prev = this . cancel ( ) ; if ( typeof task === 'function' ) { if ( delay && typeof delay === 'string' ) delay = + delay ; if ( typeof delay === 'number' && delay >= 0 ) { this . _fn = task ; this . _timeout = window . setTimeout ( this . run , delay ) ; } else { task ( ) ; } } return prev ; } /** Cancel deferred (planned) task */ public cancel ( ) { const prev = this . _fn ; ( typeof this . _timeout === 'number' ) && clearTimeout ( this . _timeout ) ; this . _fn = this . _timeout = null ; return prev ; } }