I'm working with Navigation timing object to calculate full page delay between navigation start and load event end.
My goal is to get performance.timing.loadEventEnd - performance.timing.navigationStart;
when load event is done.
However, invoking this code inside load event, load event end can not be measured. So, I need to defer it and run outside of load event.
Here are my requirements
- calling
myfunction
after load event ends. - I don't want to use timing function like
setTimeout
. IfsetTimeout
guarantee that it does not wake up during other load event, it is ok to use. - I don't want to calculate time delay inside
load
event. (like callnew Date().getTime()
orperformance.now()
at the top ofload
event, and call it again before finish and subtract it.) Because I use bunch of 3rd party library which also call load event. I can't handle all of them. - should work cross browser environment, IE>=10
$(window).ready(function(){
console.log("domContentLoadedEventStart: "
+performance.timing.domContentLoadedEventStart);
console.log("domContentLoadedEventEnd: "
+performance.timing.domContentLoadedEventEnd);
});
// result:
// > domContentLoadedEventStart: 1451979544555
// > domContentLoadedEventEnd: 0
$(window).load(function(){
console.log("domcomplete: "+performance.timing.domComplete);
console.log("loadEventStart: "+performance.timing.loadEventStart);
console.log("loadEventEnd: "+performance.timing.loadEventEnd);
});
// result:
// > domcomplete: 1451979906417
// > loadEventStart: 1451979906417
// > loadEventEnd: 0
EDIT
I've tested this suites. This test cases were intended to wake up setTimeout
during load function callback.
// myfunction which will be called by setTimeout in firstLoadCallback.
function myfunction(){
console.log("called myfunction");
}
// first load callback
$(window).load(function firstLoadCallback(){
var startTicks = performance.now();
// register myfunction with setTimeout
setTimeout(myfunction, 0);
// sleep +500ms
while(performance.now() - startTicks < 500){
;
}
var diffTicks = performance.now() - startTicks;
console.log("first ticks: "+diffTicks);
});
// second load callback
$(window).load(function secondLoadCallback(){
var startTicks = performance.now();
// sleep +500ms
while(performance.now() - startTicks < 500){
;
}
var diffTicks = performance.now() - startTicks;
console.log("second ticks: "+diffTicks);
});
// third callback from other file: other-file.js
$(window).load(function thirdLoadCallback(){
var startTicks = performance.now();
// sleep +500ms
while(performance.now() - startTicks < 500){
;
}
var diffTicks = performance.now() - startTicks;
console.log("third ticks: "+diffTicks);
});
// result:
// first ticks: 500.005
// second ticks: 500.0050000000001
// third ticks: 500.0049999999999
// called myfunction
From this result, callback which registered by setTimeout
does not wake up before function call tree end. If this result guarantee of working cross browser, @BenjaminGruenbaum answer might be correct.
I will post another question about it.