first-input-delay
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -16,3 +16,3 @@ /* | ||
(function(){function g(a,c){b||(b=a,f=c,h.forEach(function(a){removeEventListener(a,l,e)}),m())}function m(){b&&f&&0<d.length&&(d.forEach(function(a){a(b,f)}),d=[])}function n(a,c){function k(){g(a,c);d()}function b(){d()}function d(){removeEventListener("pointerup",k,e);removeEventListener("pointercancel",b,e)}addEventListener("pointerup",k,e);addEventListener("pointercancel",b,e)}function l(a){if(a.cancelable){var c=performance.now(),b=a.timeStamp;b>c&&(c=+new Date);c-=b;"pointerdown"==a.type?n(c, | ||
a):g(c,a)}}var e={passive:!0,capture:!0},h=["click","mousedown","keydown","touchstart","pointerdown"],b,f,d=[];h.forEach(function(a){addEventListener(a,l,e)});window.perfMetrics=window.perfMetrics||{};window.perfMetrics.onFirstInputDelay=function(a){d.push(a);m()}})(); | ||
(function(){function g(a,b){d||(d=!0,f=a,h=b,k.forEach(function(a){removeEventListener(a,l,e)}),m())}function m(){d&&0<c.length&&(c.forEach(function(a){a(f,h)}),c=[])}function n(a,b){function c(){g(a,b);f()}function d(){f()}function f(){removeEventListener("pointerup",c,e);removeEventListener("pointercancel",d,e)}addEventListener("pointerup",c,e);addEventListener("pointercancel",d,e)}function l(a){if(a.cancelable){var b=a.timeStamp;b=Math.max((1E12<b?+new Date:performance.now())-b,0);"pointerdown"== | ||
a.type?n(b,a):g(b,a)}}var e={passive:!0,capture:!0},k=["click","mousedown","keydown","touchstart","pointerdown"],d=!1,f,h,c=[];k.forEach(function(a){addEventListener(a,l,e)});window.perfMetrics=window.perfMetrics||{};window.perfMetrics.onFirstInputDelay=function(a){c.push(a);m()}})(); |
{ | ||
"name": "first-input-delay", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A JavaScript library for measuring the First Input Delay metric. See https://goo.gl/1AKcj5.", | ||
@@ -5,0 +5,0 @@ "main": "src/first-input-delay.js", |
@@ -17,3 +17,3 @@ # First Input Delay | ||
**1) Add the minified code in [`dist/first-input-delay.js`](/dist/first-input-delay.js) to the `<head>` of your document.** | ||
**1) Add the minified code in [`dist/first-input-delay.js`](/dist/first-input-delay.min.js) to the `<head>` of your document.** | ||
@@ -20,0 +20,0 @@ The code in this file adds the necessary event listeners to detect the first user input, and since user input on a page can happen at any time, it's critical that this code runs as early as possible. |
@@ -26,2 +26,3 @@ /* | ||
var firstInputOccurred = false; | ||
var firstInputDelay; | ||
@@ -48,3 +49,4 @@ var firstInputEvent; | ||
function recordDelay(delay, evt) { | ||
if (!firstInputDelay) { | ||
if (!firstInputOccurred) { | ||
firstInputOccurred = true; | ||
firstInputDelay = delay; | ||
@@ -66,3 +68,3 @@ firstInputEvent = evt; | ||
function reportDelayIfReady() { | ||
if (firstInputDelay && firstInputEvent && firstInputCallbacks.length > 0) { | ||
if (firstInputOccurred && firstInputCallbacks.length > 0) { | ||
firstInputCallbacks.forEach(function(callback) { | ||
@@ -127,3 +129,2 @@ callback(firstInputDelay, firstInputEvent); | ||
if (evt.cancelable) { | ||
var now = performance.now(); | ||
var eventTimeStamp = evt.timeStamp; | ||
@@ -133,8 +134,11 @@ | ||
// a DOMHighResTimeStamp, which means we need to compare it to | ||
// Date.now() instead of performance.now(). | ||
if (eventTimeStamp > now) { | ||
now = +new Date; | ||
} | ||
// Date.now() instead of performance.now(). To check for that we assume | ||
// any timestamp greater than 1 trillion is a DOMTimeStamp. | ||
var now = eventTimeStamp > 1e12 ? +new Date : performance.now(); | ||
var delay = now - eventTimeStamp; | ||
// Some browsers report event timestamp values greater than what they | ||
// report for performance.now(). To avoid computing a negative | ||
// first input delay, we clamp it at >=0. | ||
// https://github.com/GoogleChromeLabs/first-input-delay/issues/4 | ||
var delay = Math.max(now - eventTimeStamp, 0); | ||
@@ -141,0 +145,0 @@ if (evt.type == 'pointerdown') { |
22445
9
155