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
myfunctionafter load event ends. - I don't want to use timing function like
setTimeout. IfsetTimeoutguarantee that it does not wake up during other load event, it is ok to use. - I don't want to calculate time delay inside
loadevent. (like callnew Date().getTime()orperformance.now()at the top ofloadevent, 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.
setTimeout(fn, 0)or something similar.myfunction? Does setTimeout guarantee of it?setTimeoutwill register an event to execute in next tick. So yes. But as you have already mentioned you do not want to use setTimeout, you can look intopromisesetTimeouthas to be inside of the$(window).loadevent.setTimeoutin my first load callback. After that, my second load callback starts running. What if second function takes too much time? DoessetTimeoutwakes up while second function is running? @BenjaminGruenbaum Yes, I put it in the right position.