Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure time elapsed on Javascript? [duplicate]

Tags:

javascript

I created a simple game that start and ends the timer when the user finishes clicking on 16 boxes.

I want to measure the elapsed time for the user to complete the game. How do I do it using Javascript?

I took a look at different answers like this, but I had hard time understanding others' code.

I would assume it to look like this.

Timer Start: When user clicks the first box Timer End: When user clicks the last box 
like image 261
Leonard Avatar asked Jan 13 '17 10:01

Leonard


People also ask

How do you calculate elapsed time in JavaScript?

getTime() - startTime. getTime(); Note: the getTime() built-in date function returns the elapsed time between Jan 1st, 1970 00:00:00 UTC and the given date.

How do you measure elapsed time?

In simple words, we can say that the amount of time that has passed between the beginning and the end of an event is called the elapsed time. We can determine this time by subtracting the end time and the start time. The formula to calculate the elapsed time is simply to subtract the hours and minutes separately.

What are the two ways to find the elapsed time?

Subtract the minutes and hours separately. For example to calculate the elapsed time between 12:10 and 16:40, subtract 12:10 from 16:4. Looking at the hours, 16 – 12 = 4 and looking at the minutes, 40 – 10 = 30.


1 Answers

The Date documentation states that :

The JavaScript date is based on a time value that is milliseconds since midnight January 1, 1970, UTC

Click on start button then on end button. It will show you the number of seconds between the 2 clicks.

The milliseconds diff is in variable timeDiff. Play with it to find seconds/minutes/hours/ or what you need

var startTime, endTime;    function start() {    startTime = new Date();  };    function end() {    endTime = new Date();    var timeDiff = endTime - startTime; //in ms    // strip the ms    timeDiff /= 1000;      // get seconds     var seconds = Math.round(timeDiff);    console.log(seconds + " seconds");  }
<button onclick="start()">Start</button>    <button onclick="end()">End</button>

OR another way of doing it for modern browser

Using performance.now() which returns a value representing the time elapsed since the time origin. This value is a double with microseconds in the fractional.

The time origin is a standard time which is considered to be the beginning of the current document's lifetime.

var startTime, endTime;    function start() {    startTime = performance.now();  };    function end() {    endTime = performance.now();    var timeDiff = endTime - startTime; //in ms     // strip the ms     timeDiff /= 1000;         // get seconds     var seconds = Math.round(timeDiff);    console.log(seconds + " seconds");  }
<button onclick="start()">Start</button>  <button onclick="end()">End</button>
like image 51
Weedoze Avatar answered Sep 21 '22 17:09

Weedoze