Skip to content

counter

Creates an observable that emits incremental numbers at a specified interval. Starts with 0 immediately after subscription, then emits 1, 2, 3, … every interval.

// Timeline:
//
// Time(s) 0 1 2 3 4 5
// tick$ 0 1 2 3 4 5 ...
//
// Explanation:
// - counter emits incrementing numbers at specified intervals
// - Starts at 0 and continues indefinitely
// - Useful for periodic tasks or animations
const tick$ = counter(100);
const valueHistory: number[] = [];
const subscription = tick$.subscribe((count) => {
valueHistory.push(count);
});
await new Promise((resolve) => {
setTimeout(resolve, 350);
});
subscription.unsubscribe();
assert.isTrue(Arr.isArrayAtLeastLength(valueHistory, 3));
assert.deepStrictEqual(valueHistory[0], 0);
assert.deepStrictEqual(valueHistory[1], 1);
assert.deepStrictEqual(valueHistory[2], 2);

View source on GitHub