| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 'use strict'; | |
| 6 | |
| 7 /** | |
| 8 * @fileoverview This file provides a JavaScript helper function that | |
| 9 * determines when the last resource was received. | |
| 10 */ | |
| 11 (function() { | |
| 12 | |
| 13 // Set the Resource Timing interface functions that will be used below | |
| 14 // to use whatever version is available currently regardless of vendor | |
| 15 // prefix. | |
| 16 window.performance.clearResourceTimings = | |
| 17 (window.performance.clearResourceTimings || | |
| 18 window.performance.mozClearResourceTimings || | |
| 19 window.performance.msClearResourceTimings || | |
| 20 window.performance.oClearResourceTimings || | |
| 21 window.performance.webkitClearResourceTimings); | |
| 22 | |
| 23 window.performance.getEntriesByType = | |
| 24 (window.performance.getEntriesByType || | |
| 25 window.performance.mozGetEntriesByType || | |
| 26 window.performance.msGetEntriesByType || | |
| 27 window.performance.oGetEntriesByType || | |
| 28 window.performance.webkitGetEntriesByType); | |
| 29 | |
| 30 // This variable will available to the function below and it will be | |
| 31 // persistent across different function calls. It stores the last | |
| 32 // entry in the list of PerformanceResourceTiming objects returned by | |
| 33 // window.performance.getEntriesByType('resource'). | |
| 34 // | |
| 35 // The reason for doing it this way is because the buffer for | |
| 36 // PerformanceResourceTiming objects has a limit, and once it's full, | |
| 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. | |
| 39 var lastEntry = null; | |
| 40 | |
| 41 /** | |
| 42 * This method uses the Resource Timing interface, which is described at | |
| 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 | |
| 45 * resources requested via XMLHttpRequest. | |
| 46 * | |
| 47 * @return {number} The time since either the load event, or the last | |
| 48 * the last resource was received after the load event. If the load | |
| 49 * event hasn't yet happened, return 0. | |
| 50 */ | |
| 51 window.timeSinceLastResponseAfterLoadMs = function() { | |
| 52 if (window.document.readyState !== 'complete') { | |
| 53 return 0; | |
| 54 } | |
| 55 | |
| 56 var resourceTimings = window.performance.getEntriesByType('resource'); | |
| 57 if (resourceTimings.length > 0) { | |
| 58 lastEntry = resourceTimings.pop(); | |
| 59 window.performance.clearResourceTimings(); | |
| 60 } | |
| 61 | |
| 62 // The times for performance.now() and in the PerformanceResourceTiming | |
| 63 // objects are all in milliseconds since performance.timing.navigationStart, | |
| 64 // so we must also get load time in the same terms. | |
| 65 var timing = window.performance.timing; | |
| 66 var loadTime = timing.loadEventEnd - timing.navigationStart; | |
| 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) { | |
| 71 return window.performance.now() - loadTime; | |
| 72 } | |
| 73 return window.performance.now() - lastEntry.responseEnd; | |
| 74 } | |
| 75 | |
| 76 })(); | |
| OLD | NEW |