Mobile app version of vmapp.org
Login or Join
Cugini213

: Javascript page load time calculation returns negative <script type="text/javascript">//<![CDATA[ // jQuery has already been loaded but this could be equivalent to // a function in <body

@Cugini213

Posted in: #Javascript #Performance

<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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Cugini213

1 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

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);
});

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme