OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @fileoverview This file provides a JavaScript helper function that | 8 * @fileoverview This file provides a JavaScript helper function that |
9 * determines when the last resource was received. | 9 * determines when the last resource was received. |
10 */ | 10 */ |
(...skipping 26 matching lines...) Expand all Loading... |
37 // new entries are not added. We're only interested in the last entry, | 37 // new entries are not added. We're only interested in the last entry, |
38 // so we can clear new entries when they're added. | 38 // so we can clear new entries when they're added. |
39 var lastEntry = null; | 39 var lastEntry = null; |
40 | 40 |
41 /** | 41 /** |
42 * This method uses the Resource Timing interface, which is described at | 42 * This method uses the Resource Timing interface, which is described at |
43 * http://www.w3.org/TR/resource-timing/. It provides information about | 43 * http://www.w3.org/TR/resource-timing/. It provides information about |
44 * timings for resources such as images and script files, and it includes | 44 * timings for resources such as images and script files, and it includes |
45 * resources requested via XMLHttpRequest. | 45 * resources requested via XMLHttpRequest. |
46 * | 46 * |
47 * | |
48 * @return {number} The time since either the load event, or the last | 47 * @return {number} The time since either the load event, or the last |
49 * the last resource was received after the load event. If the load | 48 * the last resource was received after the load event. If the load |
50 * event hasn't yet happened, return 0. | 49 * event hasn't yet happened, return 0. |
51 */ | 50 */ |
52 window.timeSinceLastResponseAfterLoadMs = function() { | 51 window.timeSinceLastResponseAfterLoadMs = function() { |
53 if (window.document.readyState !== 'complete') { | 52 if (window.document.readyState !== 'complete') { |
54 return 0; | 53 return 0; |
55 } | 54 } |
56 | 55 |
57 var resourceTimings = window.performance.getEntriesByType('resource'); | 56 var resourceTimings = window.performance.getEntriesByType('resource'); |
58 if (resourceTimings.length > 0) { | 57 if (resourceTimings.length > 0) { |
59 lastEntry = resourceTimings.pop(); | 58 lastEntry = resourceTimings.pop(); |
60 window.performance.clearResourceTimings(); | 59 window.performance.clearResourceTimings(); |
61 } | 60 } |
62 | 61 |
63 // The times for performance.now() and in the PerformanceResourceTiming | 62 // The times for performance.now() and in the PerformanceResourceTiming |
64 // objects are all in milliseconds since performance.timing.navigationStart, | 63 // objects are all in milliseconds since performance.timing.navigationStart, |
65 // so we must also get load time in the same terms. | 64 // so we must also get load time in the same terms. |
66 var timing = window.performance.timing; | 65 var timing = window.performance.timing; |
67 var loadTime = timing.loadEventEnd - timing.navigationStart; | 66 var loadTime = timing.loadEventEnd - timing.navigationStart; |
68 if (loadTime > lastEntry.responseEnd) { | 67 |
| 68 // If there have been no resource timing entries, or the last entry was |
| 69 // before the load event, then return the time since the load event. |
| 70 if (!lastEntry || lastEntry.responseEnd < loadTime) { |
69 return window.performance.now() - loadTime; | 71 return window.performance.now() - loadTime; |
70 } | 72 } |
71 return window.performance.now() - lastEntry.responseEnd; | 73 return window.performance.now() - lastEntry.responseEnd; |
72 } | 74 } |
73 | 75 |
74 })(); | 76 })(); |
OLD | NEW |