JavaScript
Article

jQuery Get Timestamp getTime() Example

By Sam Deering

Simple jQuery code snippet to get the time since an event happened. Useful for tracking how long the user is taking to interact with the page elements.

var last, diff;
$('div').click(function(event) {
  if ( last ) {
    diff = event.timeStamp - last
    $('div').append('time since last event: ' + diff + '
'); } else { $('div').append('Click again.
'); } last = event.timeStamp; });

or simply

new Date().getTime()

Don’t forget to put your jQuery code inside a document ready function. See 4 different jQuery document ready examples for more info.

Also see: Change CSS with jQuery

Source

  • Anonymous

    event.timeStamp is not working in opera. Do you have any workarounds?
    thx

    • http://jquery4u.com/ jQuery4u

      use new Date().getTime()

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.