Skip to main content

createInterval

createInterval function generates a read-only signal that emits values at a regular interval. This is useful for scenarios where you want to receive updates or triggers periodically.

createInterval(period = 0, initialValue = 0, options: SignalIntervalOptions = {}): CreateIntervalResponse

Parameters

period The time interval (in milliseconds) between each emitted value
initialValueThe initial value to emit before starting the regular interval emissions. Optional. Default is 0
optionsOptional. The combination of CreateEffectOptions and CreateSignalOptions (excluding the allowSignalWrites and manualCleanup properties)

Returns CreateIntervalResponse

interval: Signal<boolean> A read-only signal that emits values at a regular interval.
resetInterval: functionA function that resets the interval whenever it is called.

Example

Basic Usage

ŒComponent({
template: 'Interval: {{ intervalRef.interval() }}'
})
export class TodoExampleComponent {
readonly intervalRef = createInterval(0, 2500);

constructor() {
readonly value = this.intervalRef.interval();
console.log('Value:', value);

effect(() => {
const value = this.intervalRef.interval();
// do something when it ticks...
});
}

reset() {
this.intervalRef.resetInterval();
}
}

Usage at outside of Injection Context

@Injetable()
export class TodoExampleComponent {
private readonly injector = inject(Injector);

create(): CreateIntervalResponse {
return createInterval(0, 2500, { injector: this.injector });
}
}
tip

createInterval automatically clears itself when the given injector instance is destroy.