user1361076
user1361076

Reputation:

javascript/timing

I want to calculate the time difference between the click of two buttons using java script

Upvotes: 0

Views: 1086

Answers (3)

Sam Dutton
Sam Dutton

Reputation: 15269

Also consider performance.now() for higher resolution timing.

Upvotes: 0

Alnitak
Alnitak

Reputation: 339796

Here's a hint - use:

var t = Date.now();

to get the current time (measured in milliseconds since 00:00:00 UTC on 01/01/1970) with millisecond resolution.

Note that the result will still be limited in accuracy by the system hardware.

On older browsers without Date.now(), use:

var t = new Date().valueOf();

or the terser

var t = +new Date();

The latter uses numeric coercion (the + prefix) to generate an automatic call to .valueOf().

Upvotes: 2

Alexander Pavlov
Alexander Pavlov

Reputation: 32286

  • Get current time from the epoch in milliseconds: new Date().getTime().
  • Difference between times: endTime - startTime.
  • Show message box: alert("Message").

Guess these can be combined together as needed.

Upvotes: 1

Related Questions