navigationStart time set to 0 when using HTML5 Navigation Timing API

1.9k Views Asked by At

I am using HTML5 Navigation Timing API to measure the user perceived page load time on my site using the following code:

//  Process load time using "Navigation Timing"  
function recordLoadTime ()
{
    if (typeof(window.performance) !== "undefined" 
        && typeof(window.performance.timing) !== "undefined")
    {
        if(window.performance.timing.loadEventEnd > 0)
        {
            var time = window.performance.timing.loadEventEnd  
                   - window.performance.timing.navigationStart;
        } else {
            setTimeout(recordLoadTime, 1000);
        }
    }
}

The time variable is also added to a cookie and recorded on the server in subsequent requests by the user.

I am facing an issue where the recorded time is very close to the current epoch time:

  • i.e. navigationStart is set to 0
  • but loadEventEnd has a non-zero value (i.e. the current epoch time)

I have seen this behavior on Chrome/11.0, Chrome/12.0, MSIE 7.0, MSIE 8.0 and MSIE 9.0

I have temporarily solved this by modifying the above code to record load time only when navigationStart is greater than 0. But I wish to record the load times for all pages served.

1

There are 1 best solutions below

3
On
window.onload = function()
{
  setTimeout(function(){
  var t = performance.timing;

}, 0);