Implements: IClosable
Description
The FixerRateTimer class represents a timer that is triggered in equal time intervals.
Important points
- It has symmetric cross-language implementation and is often used by the Pip.Services toolkit to perform periodic processing and cleanup in microservices.
 
Constructors
Creates new instance of the timer and sets its values.
publicconstructor(taskOrCallback: any = null, interval: number = null, delay: number = null)
- taskOrCallback: any - (optional) Notifiable object or callback function to call when timer is triggered.
 - interval: number - (optional) interval to trigger timer in milliseconds.
 - delay: number - (optional) delay before the first triggering in milliseconds.
 
Instance methods
close
Closes the timer.
This is required by the IClosable interface, but besides that it is identical to stop().
publicclose(context: IContext): Promise<void>
- context: IContext - (optional) execution context to trace execution through call chain.
 
getCallback
Gets the callback function that is called when this timer is triggered.
publicgetCallback(): () => void
- returns: function - callback function or null if it is not set.
 
getDelay
Gets an initial delay before the timer is triggered for the first time.
publicgetDelay(): number
- returns: number - delay in milliseconds.
 
getInterval
Gets a periodic timer triggering interval.
publicgetInterval(): number
- returns: number - interval in milliseconds
 
getTask
Gets the INotifiable object that receives notifications from this timer.
publicgetTask(): INotifiable
- returns: INotifiable - INotifiable object or null if it is not set.
 
setTask
Sets a new INotifiable object to receive notifications from this timer.
publicsetTask(value: INotifiable): void
- value: INotifiable - INotifiable object to be triggered.
 
isStarted
Checks if the timer is started.
publicisStarted(): boolean
- returns: boolean - true if the timer is started and false if it is stopped.
 
setCallback
Sets the callback function that is called when this timer is triggered.
publicsetCallback(value: () => void)
- value: function - callback function to be called.
 
setDelay
Sets an initial delay before the timer is triggered for the first time.
publicsetDelay(value: number): void
- value: number - delay in milliseconds.
 
setInterval
Sets a periodic timer triggering interval.
publicsetInterval(value: number): void
- value: number - interval in milliseconds.
 
start
Starts the timer.
Initially. the timer is triggered after delay. After that, it is triggered after interval until it is stopped.
publicstart(): void
stop
Stops the timer.
publicstop(): void
Examples
class MyComponent {
    private timer: FixedRateTimer = new FixedRateTimer(() => { this.cleanup }, 60000);
    ...
    public async open(context: IContext): Promise<void> {
        ...
        timer.start();
        ...
    }
    
    public async close(context: IContext): Promise<void> {
        ...
        timer.stop();
        ...
    }
    
    private cleanup(): void {
        ...
    }
    ...
}