counter
Creates an observable that emits incremental numbers at a specified interval.
Emits 0 in the macrotask right after start() (which runs at creation unless
startManually is set), then 1, 2, 3, … every interval. A subscriber added
after that 0 has been emitted does not receive it.
Options
Section titled “Options”startManually(defaultfalse): wait forstart()instead of starting at creation.emitInitial(defaulttrue): set tofalseto skip the 0 emitted right afterstart(), so the first value is 1, one interval later.counter(ms)corresponds to RxJStimer(0, ms);counter(ms, { emitInitial: false })corresponds tointerval(ms).
Example
Section titled “Example”// 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.isMinLengthArray(3, valueHistory));
assert.strictEqual(valueHistory[0], 0);
assert.strictEqual(valueHistory[1], 1);
assert.strictEqual(valueHistory[2], 2);