W3cubDocs

/RxJS

timestamp

function stable

Attaches a timestamp to each item emitted by an observable indicating when it was emitted

timestamp<T>(scheduler: SchedulerLike = async): OperatorFunction<T, Timestamp<T>>

Parameters

scheduler

Optional. Default is async.

Type: SchedulerLike.

Returns

OperatorFunction<T, Timestamp<T>>:

Description

The timestamp operator maps the source observable stream to an object of type {value: T, timestamp: R}. The properties are generically typed. The value property contains the value and type of the source observable. The timestamp is generated by the schedulers now function. By default it uses the async scheduler which simply returns Date.now() (milliseconds since 1970/01/01 00:00:00:000) and therefore is of type number.

timestamp marble diagram

Example

In this example there is a timestamp attached to the documents click event.

import { fromEvent } from 'rxjs';
import { timestamp } from 'rxjs/operators';

const clickWithTimestamp = fromEvent(document, 'click').pipe(
  timestamp()
);

// Emits data of type {value: MouseEvent, timestamp: number}
clickWithTimestamp.subscribe(data => {
  console.log(data);
});

© 2015–2018 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/timestamp