jaggs
jaggs

Reputation: 308

Calculate the time taken until a submit button is clicked

I have a form fields in a web page to be filled out.i need to calculate the time difference from when the page is opened until the submit button is clicked the time difference calculated will be stored in the same bean as for fields

I knew on how to get the time in JSP using

<jsp:useBean id="today" class="java.util.Date" />
<p>Time: <strong><fmt:formatDate type="time" value="${today}" /></strong></p>

But how to find the submit button on click event is triggered and to find the time difference ?

Any suggestions are welcome.

Thanks for your time ..

Upvotes: 2

Views: 1930

Answers (1)

Arslan Akram
Arslan Akram

Reputation: 1516

You can achieve this by creating a new Date() object on $(window).load and comparing it with another Date() object that's created on clicking the button.

Here's the Code:

// Run when the page is fully loaded including graphics. 
$( window ).load(function() {
	var timeStart = new Date();

  $("#submit").on('click', function() {
    var timeEnd = new Date();
    var timeDiff = timeEnd - timeStart;
    console.log(timeDiff);

    $("#time").html('Time Difference: ' + timeDiff + ' ms');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <button id="submit" type="submit">Submit</button>
</form>
<h1 id="time"></h1>

Here's the codepen link

Hope this answers your question.

Upvotes: 2

Related Questions