2
<script type="text/javascript">//<![CDATA[
// jQuery has already been loaded but this could be equivalent to 
// a function in <body onLoad="">
$(function(){
  var iNavStart=performance.timing.navigationStart;
  var iNavEnd=performance.timing.loadEventEnd;
  var iPageLoad=iNavEnd-iNavStart;
  console.dir({'iNavStart',iNavStart});
  console.dir({'iNavEnd',iNavEnd});
  console.dir({'iPageLoad',iPageLoad});
});
//]]</script>

When the page loads, the console reveals:

iNavStart: 1449594976150
iNavEnd:   0
iPageLoad: -1449594976150

I was expecting this calculation to give me a nice number of milliseconds. Surely iNavEnd shouldn't be 0?

Tested in Chrome 47 and Firefox 42 (both support performance.timing).

Any ideas where I'm going wrong?

2

I believe that Ionut Popa answers this on Stackoverflow in Measuring Site Load Times via performance api:

You need to measure the loadEventEnd after the onload event has finished or else it will be reported as 0, as never happened. (jquery example for attaching to the onload event)

$(window).load(function(){
 setTimeout(function(){
 window.performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {};
 var timing = performance.timing || {};
 var parseTime = timing.loadEventEnd - timing.responseEnd;
 console.log('Parsetime: ', parseTime);
 }, 0);
});
  • Thanks Stephen, its the setTimeout part I was missing that makes all the difference! – richhallstoke Dec 9 '15 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.