Skip to content

Instantly share code, notes, and snippets.

@shaekuronen
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaekuronen/9021131 to your computer and use it in GitHub Desktop.
Save shaekuronen/9021131 to your computer and use it in GitHub Desktop.
User Timing API
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="User Timing API" />
<meta charset="utf-8">
<title>JS Bin</title>
<!-- http://www.html5rocks.com/en/tutorials/webperformance/usertiming/ -->
</head>
<body>
<button id="get-time-button">Get Time</button>
<h4 id="current-time-display"></h4>
<h4 id="time-since-last-click-display"></h4>
</body>
</html>
get_time_button = document.getElementById("get-time-button"),
current_time_display = document.getElementById("current-time-display"),
time_since_last_click_display = document.getElementById("time-since-last-click-display"),
current_time = 0;
get_time_button.addEventListener("click", function() {
// if the current time button has not been clicked
if (current_time === 0) {
current_time = window.performance.now();
current_time_display.innerHTML = "Milleseconds since <a href='http://www.w3.org/TR/navigation-timing/#dom-performancetiming-navigationstart' target='_blank'>navigationStart</a> is : " + current_time;
} else {
// create local variable to store previous time
var previous_current_time = current_time;
current_time = window.performance.now();
current_time_display.innerHTML = "Time since <a href='http://www.w3.org/TR/navigation-timing/#dom-performancetiming-navigationstart' target='_blank'>navigationStart</a> is : " + current_time;
time_since_last_click_display.innerHTML = "Milleseconds since Get Time button last clicked: " + (current_time - previous_current_time);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment