YouTip LogoYouTip

Jq Event Timestamp

## jQuery event.timeStamp Property The `event.timeStamp` property returns the number of milliseconds elapsed between the time the event was triggered and January 1, 1970 (epoch time), or the time elapsed since the document was loaded, depending on the browser and jQuery version. This property is highly useful for measuring the duration between two user interactions, rate-limiting events, or debugging performance. --- ## Syntax ```javascript event.timeStamp ``` ### Parameter Values | Parameter | Description | | :--- | :--- | | `event` | **Required**. The event object passed into the event handler function. | --- ## Code Examples ### Example 1: Displaying the Timestamp of a Click Event The following example displays the timestamp when a button is clicked. ```javascript $("button").click(function(event) { $("span").text("Event triggered at: " + event.timeStamp + " ms"); }); ``` ### Example 2: Calculating the Time Elapsed Between Two Clicks You can use `event.timeStamp` to calculate the exact duration (in milliseconds) between consecutive click events. ```html

``` --- ## Technical Considerations & Best Practices 1. **Inconsistent Baselines across Browsers**: Historically, some browsers returned a Unix epoch timestamp (milliseconds since Jan 1, 1970), while others returned a high-resolution timestamp relative to the page load (`performance.now()`). * Modern browsers and modern jQuery versions (3.x+) standardize this to return a high-resolution timestamp relative to the document load time (DOMHighResTimeStamp). 2. **Performance Benchmarking**: If you need to measure precise execution times for performance profiling rather than user interaction intervals, consider using the native `performance.now()` API. 3. **Event Delegation**: When using event delegation (e.g., `$(document).on('click', 'button', function(event) {})`), `event.timeStamp` represents the time the event was originally created, not when it bubbled up to the handler.
← Jq Event TypeEvent Result β†’